JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1818
  • Score
    100M100P100Q108000F
  • License MIT

ES7 async/await ready http client

Package Exports

  • requisition
  • requisition/lib/request
  • requisition/lib/response

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 (requisition) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

requisition

NPM version Build status Test coverage Dependency Status License Downloads Gittip

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.

var req = require('requisition');

// GET a JSON body
async function () {
  var res = await req('/users.json');
  var body = await res.json();
}

// POST an image file
async function () {
  var res = await req.post('/images.json').sendFile('image.png');
  var body = await res.json();
}

API

var request = require('requisition');

Request

request(url)

GET a url, return the response object.

request[VERB](url)

VERB a url, specifically when it's not GET.

request().then( response => )

Send the request and wait for the response.

request('/').then(function (response) {

})

request().set(headers)

Set an object of headers.

request().set(header, value)

Set a single header.

Response

response.status

The status code of the response.

response.headers

The header object of the response.

response.is(types...)

Infer the Content-Type of the response, similar to Koa or Express' method. See type-is.

response.get(header)

Get the value for a header.

response.buffer().then( buffer => )

Return a single Buffer for the entire response.

response.text().then( text => )

Return a single String for the entire response.

response.json().then( body => )

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);
})

response.saveTo(filename).then( filename => )

Save the response to a file.

request('/file.txt').then(function (response) {
  return response.saveTo('/tmp/file.txt');
}).then(function () {
  console.log('file saved!');
})

response.dump()

Dumps the response. Execute this if you haven't handled the body.

response.destroy()

Destroy the response.