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/json
to ensure the correct data type - Runs
.json()
on the response - Serializes the body payload with
JSON.stringify()
- Appends
params
to the URL ofGET
requests - Sets
credentials
to'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
HEAD
request into a simple object
$ npm install fetch-json
fetchJson.get(url, params)
.then(handleData);