Skip to content

Commit 2917f09

Browse files
lifeisfoomarco-ippolito
authored andcommitted
doc: improved fetch docs
PR-URL: #57296 Refs: https://undici.nodejs.org Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d940b15 commit 2917f09

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

doc/api/globals.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,46 @@ changes:
498498
499499
A browser-compatible implementation of the [`fetch()`][] function.
500500

501+
```mjs
502+
const res = await fetch('https://nodejs.org/api/documentation.json');
503+
if (res.ok) {
504+
const data = await res.json();
505+
console.log(data);
506+
}
507+
```
508+
509+
The implementation is based upon [undici](https://undici.nodejs.org), an HTTP/1.1 client
510+
written from scratch for Node.js. You can figure out which version of `undici` is bundled
511+
in your Node.js process reading the `process.versions.undici` property.
512+
513+
## Custom dispatcher
514+
515+
You can use a custom dispatcher to dispatch requests passing it in fetch's options object.
516+
The dispatcher must be compatible with `undici`'s
517+
[`Dispatcher` class](https://undici.nodejs.org/#/docs/api/Dispatcher.md).
518+
519+
```js
520+
fetch(url, { dispatcher: new MyAgent() });
521+
```
522+
523+
It is possible to change the global dispatcher in Node.js installing `undici` and using
524+
the `setGlobalDispatcher()` method. Calling this method will affect both `undici` and
525+
Node.js.
526+
527+
```mjs
528+
import { setGlobalDispatcher } from 'undici';
529+
setGlobalDispatcher(new MyAgent());
530+
```
531+
532+
## Related classes
533+
534+
The following globals are available to use with `fetch`:
535+
536+
* [`FormData`](https://nodejs.org/api/globals.html#class-formdata)
537+
* [`Headers`](https://nodejs.org/api/globals.html#class-headers)
538+
* [`Request`](https://nodejs.org/api/globals.html#request)
539+
* [`Response`](https://nodejs.org/api/globals.html#response).
540+
501541
## Class: `File`
502542

503543
<!-- YAML

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy