Batch multiple HTTP requests together, to issue them in parallel over multiple TCP connections. Note that you can set batch size with the batch per host option.
Parameter | Type | Description |
---|---|---|
requests | array | object | An array or object containing requests, in string or object form |
When each request is specified as an array, the order of the arguments for each request is as follows:
Position | Name | Type | Description |
---|---|---|---|
1 | method | string | Mandatory. The HTTP method of the request. One of GET, POST, PUT, PATCH, DELETE, HEAD or OPTION. |
2 | url | string / HTTP URL | Mandatory. The URL to request. |
3 | body (optional) | string / object / ArrayBuffer | The body of the request if relevant. Can be set to null if not applicable but you want to set the last params argument. |
4 | params (optional) | object | Params like auth, custom headers and tags. |
Returns
Type | Description |
---|---|
object | The returned object contains Response objects. It is an array when users pass an array as requests and is an ordinary object with string keys when named requests are used (see below). |
Example with request as an array
Example batching three URLs for parallel fetching
Example with request objects
You can also use objects to hold information about a request. Here is an example where we do that in order to send a POST request, plus use custom HTTP headers by adding a Params object to the request:
Note that the requests in the example above may happen in any order, or simultaneously. When running requests in batches, there is no guarantee that e.g. req1 will happen before req2 or req3
Example with named requests
Finally, you can also send in named requests by using an object instead of an array as the parameter to http.batch(). In the following example we do this, and we also show that it is possible to mix string URLs and request objects