How to Set Header and Options in Axios

Passing headers with axios POST request

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
})
})

How to set headers in axios, ReactNative

You must pass an object with headers like documentation says: https://axios-http.com/docs/req_config

so your request will be:

axios.post('http://192.168.8.143:8000/transactions/',{formData}, {
//example with bearer token
headers: {
'Authentication': 'Bearer 89324u2jhjwe98'
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

Reactjs : Header in axios

add a null as the second parameter

await axios
.post(url, null, {headers:
{email:credentials.email}
}

axios#post(url[, data[, config]])

How to set headers with axios

You can set headers in axios as follows:

const headers = {
'Content-Type': 'application/json',
'X-Auth-Token': '97e0d315477f435489cf04904c9d0e6co',
};
axios.get(url, {headers})

How to add a header field with value as api key in react axios request

Try this.

    const AuthStr = 'Bearer '.concat(USER_TOKEN); 
axios.get(URL, { headers: { Authorization: AuthStr } })
.then(response => {
// If request is good...
console.log(response.data);
})
.catch((error) => {
console.log('error ' + error);
});


Related Topics



Leave a reply



Submit