fetch-json v
A wrapper around Fetch just for JSON
Why would you fetch anything but json? ;)
fetch-json makes REST easy. It's a lightweight JavaScript library to reduce the boilerplate code needed to make HTTP calls to JSON endpoints.
- Adds the HTTP header
Content-Type: application/jsonto ensure the correct data type - Runs
.json()on the response - Serializes the body payload with
JSON.stringify() - Appends
paramsto the URL ofGETrequests - Sets
credentialsto'same-origin'(support user sessions in frameworks like Grails, Rails, PHP, Django, Flask, etc.) - Converts the HTTP response to JSON if it's not already JSON (convenient for handling HTTP errors)
- Maps HTTP response headers from a
HEADrequest into a simple object
$ npm install fetch-json
fetchJson.get(url, params)
.then(handleData);