codekingpro/portable-devtools
114k
1# Fetch
2
3Undici exposes a fetch() method starts the process of fetching a resource from the network.
4
5Documentation and examples can be found on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/fetch).
6
7## FormData
8
9This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/FormData).
10
11If any parameters are passed to the FormData constructor other than `undefined`, an error will be thrown. Other parameters are ignored.
12
13## Response
14
15This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Response)
16
17## Request
18
19This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Request)
20
21## Header
22
23This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
24
25# Body Mixins
26
27`Response` and `Request` body inherit body mixin methods. These methods include:
28
29- [`.arrayBuffer()`](https://fetch.spec.whatwg.org/#dom-body-arraybuffer)
30- [`.blob()`](https://fetch.spec.whatwg.org/#dom-body-blob)
31- [`.bytes()`](https://fetch.spec.whatwg.org/#dom-body-bytes)
32- [`.formData()`](https://fetch.spec.whatwg.org/#dom-body-formdata)
33- [`.json()`](https://fetch.spec.whatwg.org/#dom-body-json)
34- [`.text()`](https://fetch.spec.whatwg.org/#dom-body-text)
35
36There is an ongoing discussion regarding `.formData()` and its usefulness and performance in server environments. It is recommended to use a dedicated library for parsing `multipart/form-data` bodies, such as [Busboy](https://www.npmjs.com/package/busboy) or [@fastify/busboy](https://www.npmjs.com/package/@fastify/busboy).
37
38These libraries can be interfaced with fetch with the following example code:
39
40```mjs
41import { Busboy } from '@fastify/busboy'
42import { Readable } from 'node:stream'
43
44const response = await fetch('...')
45const busboy = new Busboy({
46 headers: {
47 'content-type': response.headers.get('content-type')
48 }
49})
50
51Readable.fromWeb(response.body).pipe(busboy)
52```
53 