Skip to content

Commit 5dd3ff1

Browse files
committed
Improve multipart docs
1 parent 03bc999 commit 5dd3ff1

1 file changed

Lines changed: 30 additions & 18 deletions

File tree

README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,25 +295,37 @@ See the [form-data README](https://github.com/felixge/node-form-data) for more i
295295
Some variations in different HTTP implementations require a newline/CRLF before, after, or both before and after the boundary of a `multipart/related` request (using the multipart option). This has been observed in the .NET WebAPI version 4.0. You can turn on a boundary preambleCRLF or postamble by passing them as `true` to your request options.
296296
297297
```javascript
298-
request(
299-
{ method: 'PUT'
300-
, preambleCRLF: true
301-
, postambleCRLF: true
302-
, uri: 'http://service.com/upload'
303-
, multipart:
304-
[ { 'content-type': 'application/json'
305-
, body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
306-
}
307-
, { body: 'I am an attachment' }
298+
request({
299+
method: 'PUT',
300+
preambleCRLF: true,
301+
postambleCRLF: true,
302+
uri: 'http://service.com/upload',
303+
multipart: [
304+
{
305+
'content-type': 'application/json'
306+
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
307+
},
308+
{ body: 'I am an attachment' },
309+
{ body: fs.createReadStream('image.png') }
310+
],
311+
// alternatively pass an object containing additional options
312+
multipart: {
313+
chunked: false,
314+
data: [
315+
{
316+
'content-type': 'application/json',
317+
body: JSON.stringify({foo: 'bar', _attachments: {'message.txt': {follows: true, length: 18, 'content_type': 'text/plain' }}})
318+
},
319+
{ body: 'I am an attachment' }
308320
]
309321
}
310-
, function (error, response, body) {
311-
if (error) {
312-
return console.error('upload failed:', error);
313-
}
314-
console.log('Upload successful! Server responded with:', body);
322+
},
323+
function (error, response, body) {
324+
if (error) {
325+
return console.error('upload failed:', error);
315326
}
316-
)
327+
console.log('Upload successful! Server responded with:', body);
328+
})
317329
```
318330
319331
@@ -514,10 +526,10 @@ The first argument can be either a `url` or an `options` object. The only requir
514526
* `body` - entity body for PATCH, POST and PUT requests. Must be a `Buffer` or `String`, unless `json` is `true`. If `json` is `true`, then `body` must be a JSON-serializable object.
515527
* `form` - when passed an object or a querystring, this sets `body` to a querystring representation of value, and adds `Content-type: application/x-www-form-urlencoded` header. When passed no options, a `FormData` instance is returned (and is piped to request). See "Forms" section above.
516528
* `formData` - Data to pass for a `multipart/form-data` request. See "Forms" section above.
517-
* `multipart` - (experimental) Data to pass for a `multipart/related` request. See "Forms" section above
529+
* `multipart` - array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See _Forms_ section above.
530+
* Alternatively you can pass in an object `{chunked: false, data: []}` where `chunked` is used to specify the `transfer-encoding` header of your request. In non chunked requests body streams are not allowed.
518531
* `auth` - A hash containing values `user` || `username`, `pass` || `password`, and `sendImmediately` (optional). See documentation above.
519532
* `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as JSON.
520-
* `multipart` - (experimental) array of objects which contains their own headers and `body` attribute. Sends `multipart/related` request. See example below.
521533
* `preambleCRLF` - append a newline/CRLF before the boundary of your `multipart/form-data` request.
522534
* `postambleCRLF` - append a newline/CRLF at the end of the boundary of your `multipart/form-data` request.
523535
* `followRedirect` - follow HTTP 3xx responses as redirects (default: `true`). This property can also be implemented as function which gets `response` object as a single argument and should return `true` if redirects should continue or `false` otherwise.

0 commit comments

Comments
 (0)