Trusted answers to developer questions

How to make an Axios POST request

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Axios is a promise-based HTTP client for JavaScript that can be used in front-end applications and Node.js backends.

POST request

A POST request can be made using Axios to “post” data to an endpoint. This endpoint may then use this POST request to perform a certain task or trigger an event. The HTTP post request is performed by calling axios.post().

This method requires two parameters. First, it needs the URI of the service endpoint. Second, an object which contains the properties that we want to send to our server should be passed to it.

Passing a data object as a parameter to the post method is optional; in this way, a post method is very similar to the get method.

svg viewer

Code

The following code shows a sample POST request without any data object passed to it. The code also demonstrates how the POST request can use async/await.

const axios = require('axios')

const res = await axios.post('https:sample-endpoint.com/data')

To send a data object with the POST request, simply pass the object as the second argument in the post method. The following code demonstrates this using a promise-based code:

const axios = require('axios')

axios.post('https:sample-endpoint.com/user', {
    Name: 'Fred',
    Age: '23'
  })
  .then(function (response) {
    console.log(response);
  })

RELATED TAGS

axios
post
javascript
http
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?