When using Axios, in order to pass custom headers, supply an object containing the headers as the last argument
Modify your Axios request like:
const headers = {
'Content-Type': 'application/json',
'Authorization': 'JWT fefege...'
}
axios.post(Helper.getUserAPI(), data, {
headers: headers
})
.then((response) => {
dispatch({
type: FOUND_USER,
data: response.data[0]
})
})
.catch((error) => {
dispatch({
type: ERROR_FINDING_USER
})
})
Answer from Shubham Khatri on Stack OverflowWhen using Axios, in order to pass custom headers, supply an object containing the headers as the last argument
Modify your Axios request like:
const headers = {
'Content-Type': 'application/json',
'Authorization': 'JWT fefege...'
}
axios.post(Helper.getUserAPI(), data, {
headers: headers
})
.then((response) => {
dispatch({
type: FOUND_USER,
data: response.data[0]
})
})
.catch((error) => {
dispatch({
type: ERROR_FINDING_USER
})
})
Here is a full example of an axios.post request with custom headers
var postData = {
email: "[email protected]",
password: "password"
};
let axiosConfig = {
headers: {
'Content-Type': 'application/json;charset=UTF-8',
"Access-Control-Allow-Origin": "*",
}
};
axios.post('http://<host>:<port>/<path>', postData, axiosConfig)
.then((res) => {
console.log("RESPONSE RECEIVED: ", res);
})
.catch((err) => {
console.log("AXIOS ERROR: ", err);
})
Changing the Content-Type in axios header to fix a 415 error
Axios content-type header
Removing default content-type for post
Setting header: 'Content-Type': 'application/json' is not working
Videos
SignIn = () => {
console.log('login clicked')
let data = JSON.stringify({
password: this.state.password,
username: this.state.email
})
axios.post('url', data, {
headers: {
'Content-Type': 'application/json',
}
}
)
}
This worked for me:
const formData = new FormData();
formData.append('data', new Blob([JSON.stringify(data)], { type: 'application/json'}));
formData.append('file', file);
return axios.put(`${url}`, formData)
.then((response) => { console.log(response) })
.catch((error) => { console.log(error) })
I took this from another answer of a similar issue. You can check the original answer here.
I continue to be unable to send PUT or POST requests that omit 'application/x-www-form-urlencoded' from the Content-Type header. No matter what I do it send requests with both the form header and application/json and thus my back-end receives this as form data and I can't have that. Is it possible to NOT include the form header? If so, how? I've isolated this and tried everything I can - converting to JSON, JS object, the below simple example, and every type of variation the axios function will accept.
Vue3.2.26, axios-3.5.1
Request header:
PUT /house_orders/active_order/1 HTTP/1.1 Host: 0.0.0.0:8005 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107.0 Accept: application/json, text/plain, */* Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Content-Type: application/x-www-form-urlencoded, application/json Content-Length: 32 Origin: http://localhost:8080 Connection: keep-alive Referer: http://localhost:8080/
Code:
const send_item = {quantity: '45', price: '0.99'}
const customConfig = {
headers: {
'Content-Type': 'application/json'
}
};
const res = await axios
.put(
"/house_orders/active_order/1",
send_item ,
customConfig
)
.catch((error) => {
console.error("There was an error!", error);
});
» npm install axios
It sets global default headers. Please check Global axios defaults
Normally you should make a request as below:
import qs from 'qs';
const data = { 'bar': 123 };
const options = {
method: 'POST',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: qs.stringify(data),
url,
};
axios(options);
If most of your request will have Content-Type=application/x-www-form-urlencoded header, you can set default header with:
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
And you can delete headers line from your request options.
It is basically using for fetching data from API in componentDidMount or if you using grapghQLfor fetching data then that time also you have to use 'content-type' = 'application/json' in resolver function. 'content-type' = 'application/json' that means your content must be in json format.
var options = {
"url":"",
"method": "POST",
"headers":{
"content-type":"application/json",
"cache-control":'no-cache'
},
qs:{
limit : limit
offset : offset
}
}