Skip to content

How to make an Axios POST request?

In this post let’s take a quick look at how to make a POST request in Node.js with Axios npm package. We have already taken a look at how to make GET requests using Axios npm library by scraping a web page.

Axios is a very popular JavaScript library you can use to perform HTTP requests, that works in both Browser and Node.js platforms.

Making a GET request and reading its response is easy with Axios.

axios.get("https://forecast7.com/en/19d0872d88/mumbai/")
    .then(response => {
      console.log(response.data);
}

You can make a POST request like the following sample code.

let  apiResponse = await axios({
      url: "https://dev.poopcode.com/users/jbashe",
      method: "POST",
      headers: {
        authorization: token,
      },
      data: {
        group: params.group,
        message: params.message
      },
    });

data is the request payload we send to the API via POST method. In the api these data will be retrieved as req.body.group and req.body.message

See also  Adding custom Chai assertions to your Cypress tests
, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.