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
JavaScript Double Colon (Bind Operator)
How to Count the Number of Occurrences of Each Item in an Array
Setting Innerhtml VS. Setting Value with JavaScript
Why Does the Promise Constructor Need an Executor
Which Ecmascript 6 Features Imply Strict Mode
Multiple, Sequential Fetch() Promise
Applying Classname/Onclick/Onchange Not Working on Custom React Component
How to Use Ranges in a Switch Case Statement Using JavaScript
Angular 4.3.3 Httpclient:How Get Value from the Header of a Response
How to Call a C# Function from JavaScript
Sorting a JSON Object in JavaScript
Drawing a Line with Three.Js Dynamically
How to Return a Variable from Google Maps JavaScript Geocoder Callback
Slow Assets Compilation in Development Mode
Xmlhttprequest Status 0 (Responsetext Is Empty)