Skip to content

Commit

Permalink
Mention option to pass streams into FormData (#3086)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaoodxD authored Apr 10, 2024
1 parent 48af032 commit 6481804
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ const data = {
await fetch('https://example.com', { body: data, method: 'POST', duplex: 'half' })
```

[FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) besides text data and buffers can also utilize streams via [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) objects:

```js
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
Expand Down
27 changes: 27 additions & 0 deletions docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ async function postFormRequest (port = 3001) {
}
```

### A FormData request with file stream, read the response body as text

```js
const { request } = require('undici')
const { openAsBlob } = require('fs')

async function formDataBlobRequest () {
// Make a FormData request with file stream:

const formData = new FormData()
formData.append('field', 42)
formData.set('file', await openAsBlob('./index.mjs'))

const response = await request('http://127.0.0.1:3000', {
method: 'POST',
body: formData
})
console.log(await response.body.text())

const data = await body.text()
console.log('response received', statusCode)
console.log('headers', headers)
console.log('data', data)
}

```

### A DELETE request
```js
const { request } = require('undici')
Expand Down

0 comments on commit 6481804

Please sign in to comment.