Categories
JavaScript Answers

How to call a JSON API with Node.js?

Spread the love

To call a JSON API with Node.js, we install axios.

To install it, we run

npm install axios

Then we use it by writing

const { data } = await axios.post(url, {
  auth: {
    username,
    password,
  },
});

to call axios.post to make a post request to the url with the 2nd argument as the JSON payload.

Then we get the response body from the data property of the object returned by the promise.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *