How to Use JSON with Axios

Apr 21, 2021

By default, when making a request to a server using axios, the Content-Type is set to send JSON data. The server is not held to that same standard however and may send the data back in a different format. Axios has the transformResponse to enable you to specify how you want it received on the response. Below is an example demonstrating how to make a simple GET request with Axios:

const axios = require('axios');

const res = await axios.get('', { params: { answer: 42 } });; // 'Object', means `res` is a POJO

// `` contains the parsed response body; // { args: { answer: 42 }, ... } instanceof Object; // true


When making a POST or PUT request, Axios will automatically parse the data to JSON, provided you are sending an object, and make the necessary adjustments elsewhere in the request so it can be automatically parsed once received by the server.


// Axios automatically serializes `{ answer: 42 }` into JSON.
const res = await'', { answer: 42 });; // '{"answer":42}'['Content-Type']; // 'application/json;charset=utf-8',


const res = await axios.put('', { hello: 'world' });; // { hello: 'world' }

More Axios Tutorials