Package Exports
- then-request
- then-request/lib/handle-qs.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (then-request) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
then-request
A request library that returns promises, inspired by request
Installation
npm install then-requestUsage
request(method, url, options, callback?)
e.g.
request('GET', 'http://example.com').done(function (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 aBufferorString(only strings are accepted client side)json- setsbodybut to JSON representation of value and addsContent-type: application/json. 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).
Callback / Returns:
If a callback is provided it is called with err and res. If no callback is provided, a Promise is returned that eventually resolves to res. The resulting Promise also has an additional .getBody(encoding?) method that is equivallent to calling .then(function (res) { return res.getBody(); }).
Response
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 server
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;
}License
MIT