Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/debitoor/is-ok
Check if a HTTP response is successful
https://github.com/debitoor/is-ok
Last synced: 12 days ago
JSON representation
Check if a HTTP response is successful
- Host: GitHub
- URL: https://github.com/debitoor/is-ok
- Owner: debitoor
- Created: 2015-04-29T09:05:19.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-04-29T10:48:55.000Z (over 9 years ago)
- Last Synced: 2024-04-28T07:28:39.686Z (7 months ago)
- Language: JavaScript
- Size: 125 KB
- Stars: 1
- Watchers: 41
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# is-ok [![Build Status](https://travis-ci.org/e-conomic/is-ok.svg)](https://travis-ci.org/e-conomic/is-ok)
Check if a HTTP response is successful.
npm install is-ok
## Usage
Used together with [request](https://github.com/request/request) or the built-in `http` module.
Accepts a response object, with a `statusCode` property, as first argument and returns `true` if the status code was within the `2xx` range, otherwise it returns `false`.
Optionally it takse a callback as second argument, which is called with an error object only if the response was non-successful. The error has `statusCode`, `headers` and `body` properties set, copied from the response.
```javascript
var request = require('request');
var isOk = require('is-ok');var fetch = function(url, callback) {
request.get(url, function(err, response, body) {
if(err) return callback(err);
if(!isOk(response, callback)) return;callback(null, body);
});
}
```## License
[MIT](http://opensource.org/licenses/MIT)