http-ok
A simple promise based http client which rejects the promise when the status is not 200 - OK
Motivation
Instead of having if-statements to check the http status code when a request returns, http-ok rejects the promise when the status code is not as expected (by default 200). This should make the promise chain look much simpler and more focused on the succes flow.
Example using 'http-ok':
const HttpOk = const client = ; client ;
Example using a http-client like 'node-fetch':
const fetch = ; ;
Features
- More natural promise chain by rejecting the promise when the response code is not OK
- Stay consistent with
node http
API. Uses the same request options object. - Use native promise.
Difference from node-fetch
- See Link to Header for details.
Install
npm install http-ok --save
Usage
plain text or html
const HttpOk = ;const client = ; client ;
json response
client ;
post with form data and custom headers
const querystring = ;const formData = name: 'Bob Mc Bobson' place: 'Cheddar' ;const postData = querystring; const requestOptions = hostname: 'http://httpbin.org' headers: 'Content-Type': 'application/x-www-form-urlencoded' 'Content-Length': Buffer ; client ;
specific success case eg. 302
client ;
See test cases for more examples.
API
-
get(url) // default 200
-
get(url, expectedStatusCode)
-
get(options, expectedStatusCode)
-
post(url, postData) // default 200
-
post(url, postData, expectedStatusCode)
-
post(options, postData, expectedStatusCode)
Returns a Promise
url
Should be an absolute url, eg http://example.com
options
same as node.js http.request options.
expectedStatusCode
and integer eg 200
or 301
postData
url encoded string
License
MIT