let config = {
headers: {
header1: value,
}
}
let data = {
'HTTP_CONTENT_LANGUAGE': self.language
}
axios.post(URL, data, config).then(...)
For setting default global config:
axios.defaults.headers.post['header1'] = 'value' // for POST requests
axios.defaults.headers.common['header1'] = 'value' // for all requests
For setting as default on axios instance:
let instance = axios.create({
headers: {
post: { // can be common or any other method
header1: 'value1'
}
}
})
//- or after instance has been created
instance.defaults.headers.post['header1'] = 'value'
//- or before a request is made
// using Interceptors
instance.interceptors.request.use(config => {
config.headers.post['header1'] = 'value';
return config;
});
axios.get('https://example.com/getSomething', {
headers: {
Authorization: 'Bearer ' + token //the token is a variable which holds the token
}
})
你也可以发送一个post请求。
axios.post('https://example.com/postSomething', {
email: varEmail, //varEmail is a variable which holds the email
password: varPassword
},
{
headers: {
Authorization: 'Bearer ' + varToken
}
})
我的做法是这样设置一个请求:
axios({
method: 'post', //you can set what request you want to be
url: 'https://example.com/request',
data: {id: varID},
headers: {
Authorization: 'Bearer ' + varToken
}
})