Note: Once a mixin has been called then the body cannot be reused, thus calling additional mixins on .body, e.g. .body.json(); .body.text() will result in an error TypeError: unusable being thrown and returned through the Promise rejection.
Should you need to access the body in plain-text after using a mixin, the best practice is to use the .text() mixin first and then manually parse the text to the desired format.
For more information about their behavior, please reference the body mixin from the Fetch Standard.
Common API Methods
This section documents our most commonly used API methods. Additional APIs are documented in their own files within the docs folder and are accessible via the navigation list on the left side of the docs site.
FormData besides text data and buffers can also utilize streams via Blob objects:
import { openAsBlob } from 'node:fs'const file = await openAsBlob('./big.csv')const body = new FormData()body.set('file', file, 'big.csv')await fetch('http://example.com', { method: 'POST', body })
request.duplex
half
In this implementation of fetch, request.duplex must be set if request.body is ReadableStream or Async Iterables, however, fetch requests are currently always full duplex. For more detail refer to the Fetch Standard..
response.body
Nodejs has two kinds of streams: web streams, which follow the API of the WHATWG web standard found in browsers, and an older Node-specific streams API. response.body returns a readable web stream. If you would prefer to work with a Node stream you can convert a web stream using .fromWeb().
The Fetch Standard allows users to skip consuming the response body by relying on
garbage collection to release connection resources. Undici does not do the same. Therefore, it is important to always either consume or cancel the response body.
Garbage collection in Node is less aggressive and deterministic
(due to the lack of clear idle periods that browsers have through the rendering refresh rate)
which means that leaving the release of connection resources to the garbage collector can lead
to excessive connection usage, reduced performance (due to less connection re-use), and even
stalls or deadlocks when running out of connections.
// Doconst headers = await fetch(url) .then(async res => { for await (const chunk of res.body) { // force consumption of body } return res.headers })// Do notconst headers = await fetch(url) .then(res => res.headers)
However, if you want to get only headers, it might be better to use HEAD request method. Usage of this method will obviate the need for consumption or cancelling of the response body. See MDN - HTTP - HTTP request methods - HEAD for more details.
The Fetch Standard requires implementations to exclude certain headers from requests and responses. In browser environments, some headers are forbidden so the user agent remains in full control over them. In Undici, these constraints are removed to give more control to the user.
Undici limits the number of Content-Encoding layers in a response to 5 to prevent resource exhaustion attacks. If a server responds with more than 5 content-encodings (e.g., Content-Encoding: gzip, gzip, gzip, gzip, gzip, gzip), the fetch will be rejected with an error. This limit matches the approach taken by curl and urllib3.
Sets the global dispatcher used by Common API Methods.
undici.getGlobalDispatcher()
Gets the global dispatcher used by Common API Methods.
Returns: Dispatcher
undici.setGlobalOrigin(origin)
origin string | URL | undefined
Sets the global origin used in fetch.
If undefined is passed, the global origin will be reset. This will cause Response.redirect, new Request(), and fetch to throw an error when a relative path is passed.
Undici will only use pipelining if configured with a pipelining factor
greater than 1.
Undici always assumes that connections are persistent and will immediately
pipeline requests, without checking whether the connection is persistent.
Hence, automatic fallback to HTTP/1.0 or HTTP/1.1 without pipelining is
not supported.
Undici will immediately pipeline when retrying requests after a failed
connection. However, Undici will not retry the first remaining requests in
the prior pipeline and instead error the corresponding callback/promise/stream.
Undici will abort all running requests in the pipeline when any of them are
aborted.
Since it is not possible to manually follow an HTTP redirect on the server-side,
Undici returns the actual response instead of an opaqueredirect filtered one
when invoked with a manual redirect. This aligns fetch() with the other
implementations in Deno and Cloudflare Workers.
If you experience problem when connecting to a remote server that is resolved by your DNS servers to a IPv6 (AAAA record)
first, there are chances that your local router or ISP might have problem connecting to IPv6 networks. In that case
undici will throw an error with code UND_ERR_CONNECT_TIMEOUT.
If the target server resolves to both a IPv6 and IPv4 (A records) address and you are using a compatible Node version
(18.3.0 and above), you can fix the problem by providing the autoSelectFamily option (support by both undici.request
and undici.Agent) which will enable the family autoselection algorithm when establishing the connection.