How to send body data and headers with axios get request?
11,735
Solution 1
// data
is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', 'DELETE , and 'PATCH'
https://stackoverflow.com/a/54008789
Solution 2
As far as I know you can't send body data with GET request. With get you can have only Headers. Just simply change to POST and then you can do something like this :
const bodyParameters = {
key: "value",
};
const config = {
headers: { Authorization: `Bearer ${userToken}` },
};
axios.post("http://localhost:5000/user", bodyParameters, config)
.then((res)=> {
console.log(res)
})
.catch((err) => console.log(err));
};
or if you want to send headers with GET request
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
Author by
Admin
Updated on December 20, 2022Comments
-
Admin over 1 year
I've tried
axios.get(url, {headers:{},data:{}})
But it doesn't work with this.
-
user37309 over 3 yearsGET Body is allowed by the standards past 2014
-
mostafa.S over 2 yearsunfortunately, data in GET method is not considered as body. apparently Axios doesn't support request body for GET method. weirdly, tools like Postman easily support it. I am also looking for a solution.