HttpClientRequest
Describes immutable outgoing HTTP client requests.
HttpClientRequest is the request model shared by Effect HTTP clients and platform adapters. A request stores its method, URL, query parameters, hash, headers, and body as structured data. This module includes constructors, helpers for updating requests, body encoders for common payloads, and conversions to and from Web Request values.
Combinators
Signature
declare const accept: {
(mediaType: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, mediaType: string): HttpClientRequest;
};acceptJson
Sets the Accept header to application/json.
Signature
declare const acceptJson: (self: HttpClientRequest) => HttpClientRequest;Appends a URL segment to the request URL, inserting or trimming one slash as needed.
Signature
declare const appendUrl: {
(path: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, path: string): HttpClientRequest;
};appendUrlParam
Appends one query parameter value without removing existing values for the same name.
Signature
declare const appendUrlParam: {
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, key: string, value: string): HttpClientRequest;
};appendUrlParams
Appends query parameters from an input collection without removing existing values for matching names.
Signature
declare const appendUrlParams: {
(input: Input): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, input: Input): HttpClientRequest;
};Sets the Authorization header using HTTP Basic authentication credentials.
Signature
declare const basicAuth: {
(
username: string | Redacted<string>,
password: string | Redacted<string>,
): (self: HttpClientRequest) => HttpClientRequest;
(
self: HttpClientRequest,
username: string | Redacted<string>,
password: string | Redacted<string>,
): HttpClientRequest;
};bearerToken
Sets the Authorization header using a bearer token.
Signature
declare const bearerToken: {
(token: string | Redacted<string>): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, token: string | Redacted<string>): HttpClientRequest;
};Creates a file-backed request body from a filesystem path and sets it on the request.
Signature
declare const bodyFile: {
(
path: string,
options?: {
readonly bytesToRead?: FileSystem.SizeInput;
readonly chunkSize?: FileSystem.SizeInput;
readonly contentType?: string;
readonly offset?: FileSystem.SizeInput;
},
): (self: HttpClientRequest) => Effect<HttpClientRequest, PlatformError, FileSystem>;
(
self: HttpClientRequest,
path: string,
options?: {
readonly bytesToRead?: FileSystem.SizeInput;
readonly chunkSize?: FileSystem.SizeInput;
readonly contentType?: string;
readonly offset?: FileSystem.SizeInput;
},
): Effect<HttpClientRequest, PlatformError, FileSystem>;
};bodyFormData
Sets a FormData request body.
Signature
declare const bodyFormData: {
(body: FormData): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, body: FormData): HttpClientRequest;
};bodyFormDataRecord
Creates a FormData request body from record-style entries and sets it on the request.
Signature
declare const bodyFormDataRecord: {
(entries: HttpBody.FormDataInput): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, entries: HttpBody.FormDataInput): HttpClientRequest;
};Encodes a value as a JSON request body and sets it on the request, failing with HttpBodyError if encoding fails.
Signature
declare const bodyJson: {
(body: unknown): (self: HttpClientRequest) => Effect<HttpClientRequest, HttpBodyError>;
(self: HttpClientRequest, body: unknown): Effect<HttpClientRequest, HttpBodyError>;
};bodyJsonUnsafe
Sets a JSON request body using unsafe JSON encoding.
When to use
Use when the request body is known to be JSON-serializable and a synchronous HttpClientRequest result is needed.
Gotchas
JSON encoding may throw instead of failing in the Effect error channel.
Signature
declare const bodyJsonUnsafe: {
(body: unknown): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, body: unknown): HttpClientRequest;
};bodyStream
Sets a streaming Uint8Array request body with optional content type and content length metadata.
Signature
declare const bodyStream: {
(
body: Stream<Uint8Array<ArrayBufferLike>, unknown>,
options?: {
readonly contentLength?: number;
readonly contentType?: string;
},
): (self: HttpClientRequest) => HttpClientRequest;
(
self: HttpClientRequest,
body: Stream<Uint8Array<ArrayBufferLike>, unknown>,
options?: {
readonly contentLength?: number;
readonly contentType?: string;
},
): HttpClientRequest;
};Sets a text request body with an optional content type.
Signature
declare const bodyText: {
(body: string, contentType?: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, body: string, contentType?: string): HttpClientRequest;
};bodyUint8Array
Sets a Uint8Array request body with an optional content type.
Signature
declare const bodyUint8Array: {
(body: Uint8Array, contentType?: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, body: Uint8Array, contentType?: string): HttpClientRequest;
};bodyUrlParams
Sets an application/x-www-form-urlencoded request body from URL parameter input.
Signature
declare const bodyUrlParams: {
(input: Input): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, input: Input): HttpClientRequest;
};Applies request options to an HttpClientRequest, returning a new request.
Signature
declare const modify: {
(options: Options): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, options: Options): HttpClientRequest;
};prependUrl
Prepends a URL segment to the request URL, inserting or trimming one slash as needed.
Signature
declare const prependUrl: {
(path: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, path: string): HttpClientRequest;
};removeHash
Removes the URL fragment from a request.
Signature
declare function removeHash(self: HttpClientRequest): HttpClientRequest;removeHeader
Removes a single request header by name, returning a new request.
Signature
declare const removeHeader: {
(key: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, key: string): HttpClientRequest;
};schemaBodyJson
Creates a schema-based JSON body encoder that sets the encoded value on a request.
Signature
declare function schemaBodyJson<S extends Constraint>(
schema: S,
options?: ParseOptions,
): {
(
body: S["Type"],
): (self: HttpClientRequest) => Effect<HttpClientRequest, HttpBodyError, S["EncodingServices"]>;
(
self: HttpClientRequest,
body: S["Type"],
): Effect<HttpClientRequest, HttpBodyError, S["EncodingServices"]>;
};Sets the request body and updates Content-Type and Content-Length headers from the body metadata when available.
Signature
declare const setBody: {
(body: HttpBody): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, body: HttpBody): HttpClientRequest;
};Sets the URL fragment on a request without the leading #.
Signature
declare const setHash: {
(hash: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, hash: string): HttpClientRequest;
};Sets a single request header, replacing any existing value for that header.
Signature
declare const setHeader: {
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, key: string, value: string): HttpClientRequest;
};setHeaders
Sets multiple request headers from an input collection, replacing existing values with matching names.
Signature
declare const setHeaders: {
(input: Input): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, input: Input): HttpClientRequest;
};Sets the HTTP method on a request, returning a new request.
Signature
declare const setMethod: {
(method: HttpMethod): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, method: HttpMethod): HttpClientRequest;
};Sets the request URL. When given a URL, its search parameters and hash are extracted into the request's structured fields.
Signature
declare const setUrl: {
(url: string | URL): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, url: string | URL): HttpClientRequest;
};setUrlParam
Sets one query parameter, replacing existing values for that parameter name.
Signature
declare const setUrlParam: {
(key: string, value: string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, key: string, value: string): HttpClientRequest;
};setUrlParams
Sets query parameters from an input collection, replacing existing values for matching names.
Signature
declare const setUrlParams: {
(input: Input): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, input: Input): HttpClientRequest;
};Builds a URL from the request URL, query parameters, and hash, returning Option.none() if the URL is invalid.
Signature
declare function toUrl(self: HttpClientRequest): Option<URL>;updateHeaders
Transforms the request headers with the provided function, returning a new request.
Signature
declare const updateHeaders: {
(f: (headers: Headers) => Headers): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, f: (headers: Headers) => Headers): HttpClientRequest;
};Updates the request URL by applying a function to the current URL string.
Signature
declare const updateUrl: {
(f: (url: string) => string): (self: HttpClientRequest) => HttpClientRequest;
(self: HttpClientRequest, f: (url: string) => string): HttpClientRequest;
};Constructors
An empty GET request with no URL, query parameters, hash, headers, or body.
Signature
declare const empty: HttpClientRequest;Creates a GET request for the specified URL.
Signature
declare const get: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest;Creates a HEAD request for the specified URL.
Signature
declare const head: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest;Creates a request constructor for the specified HTTP method.
Signature
declare function make<M extends HttpMethod>(
method: M,
): (url: string | URL, options?: NoUrl) => HttpClientRequest;Constructs an HttpClientRequest from fully normalized request components.
Signature
declare function makeWith(
method: HttpMethod,
url: string,
urlParams: Input,
hash: Option<string>,
headers: Headers,
body: HttpBody,
): HttpClientRequest;Creates an OPTIONS request for the specified URL.
Signature
declare const options: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest;Creates a PATCH request for the specified URL.
Signature
declare const patch: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest;Creates a POST request for the specified URL.
Signature
declare const post: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest;Creates a PUT request for the specified URL.
Signature
declare const put: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest;Creates a TRACE request for the specified URL.
Signature
declare const trace: (url: string | URL, options?: Options.NoUrl) => HttpClientRequest;Converting
Converts a Web Request into an HttpClientRequest, preserving method, URL, headers, and supported request bodies.
Signature
declare function fromWeb(request: Request): HttpClientRequest;Converts an HttpClientRequest to a Web Request, failing with UrlError when the request URL is invalid.
Signature
declare function toWeb(
self: HttpClientRequest,
options?: {
readonly signal?: AbortSignal;
},
): Effect<Request, UrlError>;toWebResult
Converts an HttpClientRequest safely to a Web Request as a Result, failing when the request URL is invalid.
Signature
declare function toWebResult(
self: HttpClientRequest,
options?: {
readonly context?: Context<never>;
readonly signal?: AbortSignal;
},
): Result<Request, UrlError>;Guards
isHttpClientRequest
Returns true when a value is an HttpClientRequest.
Signature
declare function isHttpClientRequest(u: unknown): u is HttpClientRequest;Models
HttpClientRequest interface
Immutable model of an outgoing HTTP client request, including its method, URL components, headers, and body.
Signature
interface HttpClientRequest extends Inspectable, Pipeable {
readonly "~effect/http/HttpClientRequest": "~effect/http/HttpClientRequest";
readonly body: HttpBody;
readonly hash: Option<string>;
readonly headers: Headers;
readonly method: HttpMethod;
readonly url: string;
readonly urlParams: UrlParams;
}Options
Options for constructing or modifying an HttpClientRequest.
Signature
interface Options {
readonly accept?: string;
readonly acceptJson?: boolean;
readonly body?: HttpBody;
readonly hash?: string;
readonly headers?: Input;
readonly method?: HttpMethod;
readonly url?: string | URL;
readonly urlParams?: Input;
}
Sets the
Acceptheader to the specified media type.