A light, fluent node.js HTTP client for ES6. Heavily inspired by superagent as well as fetch. Designed with ES7 async/await in mind.
The API is quite minimal for now. Features will be added while people request or use them.
const req = require('requisition');
// GET a JSON body
async function () {
const res = await req('/users.json');
const body = await res.json();
}
// POST an image file
async function () {
const res = await req.post('/images.json').sendFile('image.png');
const body = await res.json();
}
This is similar to other AJAX libraries like axios except:
- Does not have browser support
- Does not depend on gigantic options objects
- Many utilities for handling HTTP responses like saving to a file
const request = require('requisition');
GET
a url
, return the response object.
VERB
a url
, specifically when it's not GET
.
Send the request and wait for the response.
request('/').then(function (response) {
})
Set an object of headers.
Set a single header.
Add basic authorization.
Set http agent.
Set timeout.
Set max redirect times.
Set header If-Modified-Since
.
Set header If-None-Match
.
Set header Content-Type
.
Set cookie, uses cookie.serialize()
request('/cookie')
.cookie(name, value, options)
.then(function (response) {
console.info(response.cookies);
})
Add query string.
Add http body.
Send file.
The status code of the response.
The header object of the response.
Infer the Content-Type
of the response,
similar to Koa or Express' method.
See type-is.
Get the value for a header.
Get charset.
Get header ETag
.
Get header Last-Modified
.
Get cookies
request('/cookie').then(function (response) {
console.info(response.cookies);
})
Return a single Buffer
for the entire response.
Return a single String
for the entire response.
Automatically parse the JSON of the response.
request('/users.json').then(function (response) {
assert(response.status === 200, 'Bad response!');
assert(response.is('json'), 'Bad type!');
return response.json()
}).then(function (body) {
console.log(body);
})
Save the response to a file.
If no filename
is specified,
saves to a random file in your temporary directory.
request('/file.txt').then(function (response) {
return response.saveTo('/tmp/file.txt');
}).then(function () {
console.log('file saved!');
})
Dumps the response. Execute this if you haven't handled the body.
Destroy the response.