xxxxxxxxxx
//set headers globally in axios
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`
xxxxxxxxxx
axios.post('url', {"body":data}, {
headers: {
'Content-Type': 'application/json'
}
}
)
xxxxxxxxxx
const username = ''
const password = ''
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
const url = 'https://...'
axios.post(url, {
headers: {
'Authorization': `Basic ${token}`
}
})
xxxxxxxxxx
var postData = {
email: "test@test.com",
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);
})
xxxxxxxxxx
// Send a POST request
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
},
headers: {'Authorization': 'Bearer ...'}
});
xxxxxxxxxx
axios.post(
'https://example.com/postSomething',
{ // this is the body
email: varEmail,
password: varPassword
},
{
headers: {
Authorization: 'Bearer ' + varToken
}
})
xxxxxxxxxx
let auth = ``; //auth key
let url = ``; //api url
const axios = require(`axios`);
axios({
method: 'get',
url: url,
headers: {
"Authorization": auth
},
}).then(function (res) {
console.log(res.data)
});
xxxxxxxxxx
const axios = require('axios');
// httpbin.org gives you the headers in the response
// body `res.data`.
// See: https://httpbin.org/#/HTTP_Methods/get_get
const res = await axios.get('https://httpbin.org/get', {
headers: {
'Test-Header': 'test-value'
}
});
res.data.headers['Test-Header']; // "test-value"
get headers values(e.g. access-token) from axios request
xxxxxxxxxx
const response = await axios({
method: 'post',
url: url,
data: data,
});
console.log(response.headers["access-token"])
xxxxxxxxxx
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
})
})
axios headers option config
xxxxxxxxxx
const config = {
headers:{
header1: value1,
header2: value2
}
};
const url = "api endpoint";
const data ={
name: "Name",
email: "email@email.com"
}
// ----If it has no body like a GET request
axios.get(url, config)
.then(res=> console.log(res))
.catch(err=> console.log(err))
// ----If it has some data to be sent
axios.post(url, data, config)
.then(res => console.log(res))
.catch(err => console.log(err))