A request library that returns promises and supports both browsers and node.js
npm install then-request
request(method, url, options, callback?)
The following examples all work on both client and server.
var request = require('then-request');
request('GET', 'http://example.com').done(function (res) {
console.log(res.getBody());
});
request('POST', 'http://example.com/json-api', {json: {some: 'values'}}).getBody('utf8').then(JSON.parse).done(function (res) {
console.log(res);
});
var FormData = request.FormData;
var data = new FormData();
data.append('some', 'values');
request('POST', 'http://example.com/form-api', {form: data}).done(function (res) {
console.log(res.getBody());
});
Or with ES6
import request, {FormData} from 'then-request';
request('GET', 'http://example.com').done((res) => {
console.log(res.getBody());
});
request('POST', 'http://example.com/json-api', {json: {some: 'values'}}).getBody('utf8').then(JSON.parse).done((res) => {
console.log(res);
});
var FormData = request.FormData;
var data = new FormData();
data.append('some', 'values');
request('POST', 'http://example.com/form-api', {form: data}).done((res) => {
console.log(res.getBody());
});
Method:
An HTTP method (e.g. GET
, POST
, PUT
, DELETE
or HEAD
). It is not case sensitive.
URL:
A url as a string (e.g. http://example.com
). Relative URLs are allowed in the browser.
Options:
qs
- an object containing querystring values to be appended to the uriheaders
- http headers (default:{}
)body
- body for PATCH, POST and PUT requests. Must be aBuffer
,ReadableStream
orString
(only strings are accepted client side)json
- setsbody
but to JSON representation of value and addsContent-type: application/json
. Does not have any affect on how the response is treated.form
- You can pass aFormData
instance to theform
option, this will manage all the appropriate headers for you. Does not have any affect on how the response is treated.cache
- only used in node.js (browsers already have their own caches) Can be'memory'
,'file'
or your own custom implementaton (see https://github.com/ForbesLindesay/http-basic#implementing-a-cache).followRedirects
- defaults totrue
but can be explicitly set tofalse
on node.js to prevent then-request following redirects automatically.maxRedirects
- sets the maximum number of redirects to follow before erroring on node.js (default:Infinity
)allowRedirectHeaders
(default:null
) - an array of headers allowed for redirects (none ifnull
).gzip
- defaults totrue
but can be explicitly set tofalse
on node.js to prevent then-request automatically supporting the gzip encoding on responses.agent
- (default:false
) - AnAgent
to controll keep-alive. When set tofalse
use anAgent
with default values.timeout
(default:false
) - times out if no response is returned within the given number of milliseconds.socketTimeout
(default:false
) - callsreq.setTimeout
internally which causes the request to timeout if no new data is seen for the given number of milliseconds. This option is ignored in the browser.retry
(default:false
) - retry GET requests. Set this totrue
to retry when the request errors or returns a status code greater than or equal to 400 (can also be a function that takes(err, req, attemptNo) => shouldRetry
)retryDelay
(default:200
) - the delay between retries (can also be set to a function that takes(err, res, attemptNo) => delay
)maxRetries
(default:5
) - the number of times to retry before giving up.
Returns:
A Promise is returned that eventually resolves to the Response
. The resulting Promise also has an additional .getBody(encoding?)
method that is equivallent to calling .then(function (res) { return res.getBody(encoding?); })
.
Note that even for status codes that represent an error, the promise will be resolved as the request succeeded. You can call getBody
if you want to error on invalid status codes. The response has the following properties:
statusCode
- a number representing the HTTP status codeheaders
- http response headersbody
- a string if in the browser or a buffer if on the serverurl
- the URL that was requested (in the case of redirects on the server, this is the final url that was requested)
It also has a method getBody(encoding?)
which looks like:
function getBody(encoding) {
if (this.statusCode >= 300) {
var err = new Error('Server responded with status code ' + this.statusCode + ':\n' + this.body.toString(encoding));
err.statusCode = this.statusCode;
err.headers = this.headers;
err.body = this.body;
throw err;
}
return encoding ? this.body.toString(encoding) : this.body;
}
var FormData = require('then-request').FormData;
Form data either exposes the node.js module, form-data, or the builtin browser object FormData, as appropriate.
They have broadly the same API, with the exception that form-data handles node.js streams and Buffers, while FormData handles the browser's File
Objects.
MIT