Axios GET with Data

Sep 8, 2020

Axios' post() function supports a data parameter that becomes the HTTP request body. On the other hand, axios.get() does not support this parameter. The 2nd parameter to axios.get() is the Axios options.

That's because, while the HTTP spec does not specifically forbid sending a request body with a GET request, older versions of the HTTP spec say that HTTP servers should ignore GET request bodies. So most HTTP services don't support GET request bodies.

Use params Instead

Instead of sending your data using the data parameter, you can use the params option to tell Axios to put your parameters in the query string:

const axios = require('axios');

// Equivalent to `axios.get('https://httpbin.org/get?answer=42')`
const res = await axios.get('https://httpbin.org/get', { params: { answer: 42 } });

res.data.args; // { answer: 42 }

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Axios Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!