如何使用多个axios标头
我正在开发本机应用程序。该项目使用FatSecret REST API。该API使用OAuth 2.0身份验证。首先,我收到访问令牌。我尝试使用令牌接收数据,但出现错误。发送帖子后,我的状态码为200,但我不明白。
let form = new FormData();
var headers = new Headers();
headers.append("content-type", "application/json"),
headers.append(
"Authorization",
"Basic " +
base64.encode(
<clientID>:<clientSecret>,
),
),
form.append("grant_type", "client_credentials");
form.append("scope", "basic");
fetch("", {
method: "POST",
headers: headers,
body: form,
})
.then(res => res.json())
.then(json =>{this.calc(json.access_token)})
};
//------------------------------------------------------
//success (get Access token)
//--------------------------------------------------------
calc =(token)=>{
console.log(token)
var headers2 = new Headers();
headers2.append("Content-Type", "application/json");
headers2.append(
"Authorization",
"Bearer "+token),
fetch(".api",{
method :"POST",
headers:headers2,
body:"method=foods.search&search_expression=toast&format=xml"
}).then(res=>console.log(res));
}
Response {type: "default", status: 200, ok: true, statusText: undefined, headers: Headers, …}
headers: Headers {map: {…}}
ok: true
status: 200
statusText: undefined
type: "default"
url: ".api"
_bodyBlob: Blob {_data: {…}}
_bodyInit: Blob {_data: {…}}
__proto__: Object
回答如下:您可以将以下代码用于带有axios的多个标头:
const instance = axios.create();
instance.defaults.headersmon['Accesstoken'] = 'Bearer '+token;
const response = await instance.post(<api url>, {
id: orderId
});