[英]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.