Sending POST JSON Requests With Axios

Sending POST JSON Requests With Axios


Axios is a promise-based HTTP client library that makes it simple to send asynchronous HTTP requests (such as POST, GET, and DELETE) to REST endpoints, mainly APIs.

In this article, we will learn how to send POST JSON requests with Axios, and how to handle both previously serialized and unserialized data.

How to Send POST JSON Requests Using Axios

The POST request is used to send data to an endpoint. For example, if we have a registration page where users submit their information, this information can be sent as JSON to the endpoint we specify using a POST JSON request.

We use the method to send a POST request with Axios, which takes two major parameters - the URL of the endpoint (url), and the object representing data we want to post (data):[, data[, config]])

Besides those two parameters, there is also a third one - config. It is used to configure the POST request we are sending, mainly to set headers.

Sending Unserialized Data

It is important to note that Axios uses JSON for data posting by default, meaning that any object we pass into Axios automatically serializes the object to JSON and sets the Content-Type header to application/json. Let's illustrate that in the example - assume we are posting the object { name: 'John Doe' }:

const result = await'', { name: 'John Doe' });

Axios will automatically serialize the object into JSON format:

console.log(; // '{"name":"John Doe"}'

And as we said, it will also set our Content-Type header to application/json:

console.log(['Content-Type']); // 'application/json;charset=utf-8'

Sending Serialized JSON String

So far, we've seen that Axios will automatically serialize whatever data we send it, but in this case, we'll send a serialized JSON string as the second parameter of the method:

const usersName = JSON.stringify({ name: 'John Doe' });

Axios will simply treat this data as a form-encoded request body rather than setting the content-type header to application/json, as shown below:

const usersName = JSON.stringify({ name: 'John Doe' });
const result = await'', usersName);

console.log(['Content-Type']); // 'application/x-www-form-urlencoded',

To resolve this issue, we need to explicitly set the Content-Type header to application/json. We can do that by passing the config argument when calling the method:

const usersName = JSON.stringify({ name: 'John Doe' });
customConfig = {
    headers: {
    'Content-Type': 'application/json'
const result = await'', usersName, customConfig);

console.log(; // '{"name":"John Doe"}'
console.log(['Content-Type']); // 'application/json',


In this article, we learned how to send POST JSON requests with Axios, fully aware that these data could be serialized or non-serialized, and we dealt with both scenarios.

Last Updated: June 21st, 2022
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Joel OlawanleAuthor

Frontend Developer & Technical Writer


React State Management with Redux and Redux-Toolkit

# javascript# React

Coordinating state and keeping components in sync can be tricky. If components rely on the same data but do not communicate with each other when...

David Landup
Uchechukwu Azubuko

Getting Started with AWS in Node.js

Build the foundation you'll need to provision, deploy, and run Node.js applications in the AWS cloud. Learn Lambda, EC2, S3, SQS, and more!

© 2013-2023 Stack Abuse. All rights reserved.