[英]Issue appending to headers object after axios request
因此,我的最初请求getBookingSessionId捕获了一个ID,该ID需要传递到axios.create()方法内的标头对象中,以便可以在下一个请求中使用。
import axios from 'axios';
import { strings } from '../strings';
class CorasApi {
constructor() {
const url = 'https://sandbox.coras.io';
this.bookingSessionId = null;
这将创建axios的实例并保存我的标头
this.request = axios.create({
baseURL: url,
headers: {
'Coras-Distributor': strings.coras.distributorId,
'Content-Type': strings.coras.contentType,
}
});
}
getBookingSessionId = () => {
return new Promise((resolve, reject) => {
this.request
.post('/booking-session/', null, null, {})
.then(res => {
// debugger;
resolve(res);
在这里,我试图将请求的结果附加到我的headers对象中
this.bookingSessionId = res.data['booking-session'];
this.request.headers.append('Booking-Session', this.bookingSessionId);
})
.catch(err => {
reject(err);
});
});
};
这是下一个要求“ Booking-Session”标头的请求
async getShows() {
await this.getBookingSessionId();
return new Promise((resolve, reject) => {
this.request
.get('/events/', null, {})
.then(res => resolve(res))
.catch(err => {
reject(err);
});
});
}
根据docs ,您可以在创建实例后更改默认值。
getBookingSessionId = () => {
return new Promise((resolve, reject) => {
this.request
.post('/booking-session/', null, null, {})
.then(res => {
resolve(res);
this.bookingSessionId = res.data['booking-session'];
// this.request.headers.append('Booking-Session', this.bookingSessionId);
this.request.defaults.headers.common['Booking-Session'] = this.bookingSessionId; // replace the line above with this
})
.catch(err => {
reject(err);
});
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.