Undici ships a complete implementation of the WHATWG Fetch Standard. It is fully compatible with the browserDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
fetch API: the same Request, Response, Headers, and FormData classes are used, and the same body mixin methods are available on both request and response objects.
Node.js has its own built-in
fetch backed by a different dispatcher symbol. Keep fetch and FormData from the same implementation — either both from undici, both from Node.js globals, or install undici’s globals with install(). Mixing them produces unexpected multipart behavior.undici.fetch(input, init)
Initiates an HTTP request following the Fetch standard. Returns a Promise<Response> that resolves when response headers are received. The response body is a Web ReadableStream and must be consumed.
Parameters
The resource to fetch. Accepts a URL string, a WHATWG
URL object, or a
Request object. When a Request is passed, the init options are merged
on top of it.Relative URLs are resolved against the global origin set by
setGlobalOrigin(). A relative URL without a
global origin set throws a TypeError.Optional request configuration.
Return value
ReturnsPromise<Response>. See the Response class section for
the full property and method reference.
Accepted body types
Thebody field of RequestInit and the Response constructor accept the
following types:
| Type | Notes |
|---|---|
string | Encoded as UTF-8 |
ArrayBuffer | Sent as raw bytes |
ArrayBufferView (e.g. Uint8Array) | Sent as raw bytes |
Blob | Content-Type set from blob.type if not already set |
FormData | Encoded as multipart/form-data |
URLSearchParams | Encoded as application/x-www-form-urlencoded |
ReadableStream | Streaming — requires duplex: 'half' |
AsyncIterable<Uint8Array> | Streaming — requires duplex: 'half' |
Streaming request bodies
To stream a request body using aReadableStream or AsyncIterable, you must
set duplex: 'half' in RequestInit. Without this field, the stream is not
accepted.
Streaming request body
Deviations from the spec
Undici’s fetch implementation follows the standard closely but differs in a few ways relevant to server-side use:- No CORS enforcement. CORS is a browser security mechanism. Undici does not reject cross-origin requests or strip response headers based on CORS rules.
- Forbidden header handling. Some headers that browsers forbid (e.g.
host) can be set or are not removed. - Content-Encoding limit. At most 5 layers of
Content-Encodingare decoded. Responses with more layers result in an error. formData()on the server. Parsingmultipart/form-datawith.formData()works but is not recommended for performance-critical paths. Prefer Busboy or @fastify/busboy instead.
Request
Represents an HTTP request. Implements the body mixin and all standard
Request properties.
Constructing a Request
Constructor
Properties
The serialized URL of the request.
The HTTP method, e.g.
'GET', 'POST'.A
Headers object containing the request headers.The request body as a Web
ReadableStream, or null if none.true after the body has been consumed.The abort signal associated with this request.
The duplex mode.
'half' when streaming bodies are used.The fetch mode (
'cors', 'no-cors', etc.).The credentials mode (
'omit', 'same-origin', 'include').The cache mode.
The redirect mode (
'follow', 'error', 'manual').The referrer URL string.
The referrer policy.
Subresource Integrity string.
Whether to keep the connection alive after the page unloads (browser context).
Methods
Returns a copy of the
Request. Throws if the body has already been consumed.Response
Represents an HTTP response. Implements the body mixin and all standard
Response properties.
Reading a Response
Properties
HTTP status code (e.g.
200, 404).HTTP status message (e.g.
'OK', 'Not Found').true when status is in the range 200–299.Response headers as a
Headers object.The final URL after any redirects.
true if the response is the result of one or more redirects.Response type:
'basic', 'cors', 'default', 'error', 'opaque', or
'opaqueredirect'.The response body as a Web
ReadableStream. Convert to a Node.js stream with
Readable.fromWeb(response.body).true after the body has been consumed.Body methods
All body methods return aPromise and consume body exactly once.
Parses the body as JSON.
Reads the body as a UTF-8 string.
Reads the body into an
ArrayBuffer.Wraps the body in a
Blob.Returns the body as a
Uint8Array.Parses the body as
multipart/form-data or
application/x-www-form-urlencoded. Not recommended for server-side
multipart parsing — use Busboy instead.Static methods
Returns a network error response.
Creates a redirect response.
status must be one of 301, 302, 303,
307, or 308.Creates a
Response with a JSON-serialized body and content-type: application/json.Methods
Returns a copy of the
Response. Throws if the body has already been consumed.Converting the response body to a Node.js stream
The response body is a WebReadableStream. To pipe it through Node.js stream
APIs, convert it with Readable.fromWeb:
Converting body to a Node.js Readable
Headers
Implements the
WHATWG Headers
interface. Used by both Request and Response.
Using Headers
Constructor
Methods
Returns the value of the header, or
null if not set. Multiple values are
joined with ', '.Sets the header, replacing any existing value.
Appends a value without replacing existing values for the same name.
Returns
true if the header exists.Removes the header.
Returns all
set-cookie header values as an array (one per value, unlike
get() which joins them).Returns an iterator of
[name, value] pairs.Returns an iterator of header names.
Returns an iterator of header values.
Calls
callback(value, name, headers) for each header.FormData
Implements the
WHATWG FormData
interface for building multipart/form-data request bodies.
Submitting a form
Constructor
undefined throws an error.
Methods
Appends a field.
value may be a string or Blob. filename applies only
when value is a Blob.Sets a field, replacing any existing entries with the same name.
Returns the first value for
name, or null.Returns all values for
name.Returns
true if a field with name exists.Removes all entries with
name.Returns an iterator of
[name, value] pairs.Returns an iterator of field names.
Returns an iterator of field values.
Calls
callback(value, name, formData) for each entry.Parsing multipart responses
For server-side multipart parsing, use Busboy or @fastify/busboy:Parsing multipart with Busboy