Package Exports
- fetch-json
- fetch-json/fetch-json.d.ts
- fetch-json/fetch-json.esm.js
- fetch-json/fetch-json.js
- fetch-json/fetch-json.min.js
- fetch-json/fetch-json.umd.cjs
Readme
fetch-json

A wrapper around Fetch just for JSON
Why would you fetch anything but json? ;)
1) Make REST Easy
fetch-json automatically:
- Converts the HTTP response to JSON if it's not already JSON (especially convenient for HTTP errors)
- Serializes the body payload with
JSON.stringify() - Adds the
application/jsonHTTP header to set the data type - Appends the GET
paramsobject items to the URL - Runs
.json()on the response - Sets
credentialsto'same-origin'(support user sessions in Grails, Rails, PHP, Django, Flask, etc.)
fetch-json is ideal for a JAMstack architecture where "dynamic programming during the request/response cycle is handled by JavaScript, running entirely on the client".
2) Setup
Web browser
In a web page:
<script src=fetch-json.min.js></script>or from the jsdelivr.com CDN:
<script src=https://cdn.jsdelivr.net/npm/fetch-json@2.4/dist/fetch-json.min.js></script>Node.js server
Install package for node:
$ npm install node-fetch fetch-jsonand then import:
import { fetchJson } from 'fetch-json';or for older CommonJS modules use:
const { fetchJson } = require('fetch-json'); //deprecated3) Examples
HTTP GET
Fetch the NASA Astronomy Picture of the Day:
// NASA APoD
const url = 'https://api.nasa.gov/planetary/apod';
const params = { api_key: 'DEMO_KEY' };
const handleData = (data) =>
console.log('The NASA APoD for today is at: ' + data.url);
fetchJson.get(url, params).then(handleData);HTTP POST
Create a resource for the planet Jupiter:
// Create Jupiter
const resource = { name: 'Jupiter', position: 5 };
const handleData = (data) =>
console.log('New planet:', data); //http response body as an object literal
fetchJson.post('https://httpbin.org/post', resource)
.then(handleData)
.catch(console.error);For more examples, see the Mocha specification cases:
spec/node.js
(Mocha output for each build under Run npm test)
To see a website that incorporates fetch-json, check out DataDashboard:
data-dashboard.js.org 📊
4) Examples Using async/await
HTTP GET
Fetch the NASA Astronomy Picture of the Day:
// NASA APoD
const show = async () => {
const url = 'https://api.nasa.gov/planetary/apod';
const params = { api_key: 'DEMO_KEY' };
const data = await fetchJson.get(url, params);
console.log('The NASA APoD for today is at: ' + data.url);
};
show();HTTP POST
Create a resource for the planet Jupiter:
// Create Jupiter
const create = async (resource) => {
const data = await fetchJson.post('https://httpbin.org/post', resource);
console.log('New planet:', data); //http response body as an object literal
};
create({ name: 'Jupiter', position: 5 });5) Leverages the Fetch API and node-fetch
fetch-json calls the native Fetch API if in a web browser and calls node-fetch if running on node.
For comparison, the above POST example in section 2 to create a planet would be done calling the Fetch API directly with the code:
// Create Jupiter (WITHOUT fetch-json)
const resource = { name: 'Jupiter', position: 5 };
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(resource),
};
const handleData = (data) =>
console.log(data); //http response body as an object literal
fetch('https://httpbin.org/post', options)
.then(response => response.json())
.then(handleData)
.catch(console.error);The example with fetch-json and the example without fetch-json each produce the same output.
6) API
API — HTTP Request
The format for using fetch-json is:
GET
fetchJson.get(url, params, options).then(callback);POST
fetchJson.post(url, resource, options).then(callback);PUT
fetchJson.put(url, resource, options).then(callback);PATCH
fetchJson.patch(url, resource, options).then(callback);DELETE
fetchJson.delete(url, resource, options).then(callback);Notes:
- Only the
urlparameter is required. The other parameters are optional. - The
paramsobject forfetchJson.get()is converted into a query string and appended to theurl. - The
resourceobject is turned into the body of the HTTP request. - The
optionsparameter is passed through to the Fetch API (see theinitdocumentation on MDN). optionsis enhanced with a boolean setting forstrictErrorsmode (defaultfalse) that throws an error to.catch()whenever the HTTP response status is 400 or higher.
Dynamic HTTP method
If you need to programmatically set the method, use the format:
fetchJson.request(method, url, data, options).then(callback);Where method is 'GET', 'POST', 'PUT', 'PATCH', or 'DELETE', and data represents
either params or resource.
API — Logging
Enable basic logging to the console with:
fetchJson.enableLogger();To use a custom logger, pass in a function that accepts 9 parameters to log.
To disable logging, pass in false.
To get an array containing the names of the parameters:
fetchJson.getLogHeaders();The default console output looks like:
2018-09-12T07:20:12.372Z – "request" - "GET" – "api.nasa.gov" – "https://api.nasa.gov/planetary/apod"
2018-09-12T07:20:13.009Z – "response" - "GET" – "api.nasa.gov" – "https://api.nasa.gov/planetary/apod" - true - 200 - "OK" - "application/json"
7) Response Text Converted to JSON
The HTTP response body is considered to be JSON if the Content-Type is "application/json" or
"text/javascript". If the HTTP response body is not JSON, fetch-json passes back
through the promise an object with a bodyText string field containing response body text.
In addition to the bodyText field, the object will have the fields: ok, status, statusText,
and contentType.
For example, an HTTP response for an error status of 500 would be converted to an object similar to:
{
ok: false,
status: 500,
statusText: 'INTERNAL SERVER ERROR',
contentType: 'text/html; charset=utf-8',
bodyText: '<!doctype html><html lang=en><body>Server Error</body></html>',
}With fetch-json, you know the response body will always be passed back to you as a simple object literal.
8) TypeScript Declarations
The TypeScript Declaration File file is fetch-json.d.ts in the dist folder.
The declarations provide type information about the API. For example, the fetchJson.post()
function returns a Promise for a FetchResponse:
fetchJson.post(url: string, resource?: RequestData,
options?: FetchOptions): Promise<FetchResponse>9) Legacy Web Browsers
To support really old browsers, include polyfills for Promise and Fetch API:
<script src=https://cdn.jsdelivr.net/npm/promise-polyfill@8.2/dist/polyfill.min.js></script>
<script src=https://cdn.jsdelivr.net/npm/whatwg-fetch@3.5/dist/fetch.umd.min.js></script>Note:
JSDOM does not include fetch, so you need to add a polyfill.
See usage of whatwg-fetch in spec/jsdom.js and gulpfile.js.
10) Contributor Notes
To be a contributor, fork the project and run the commands npm install and npm test on your
local clone. Make your edits and rerun the tests. Pull requests welcome.
"Stop trying to make fetch happen without #fetchJson!"
Feel free to submit questions at:
github.com/center-key/fetch-json/issues