如何设置从第一个请求创建的令牌&在下一个请求中使用?尝试将其设置为"X-Auth-Token": clientToken或Authorization:Bearer ${clientToken},但仍然无法工作。
得到一个错误声明:
data: {
description: "Missing request header 'X-Auth-Token' for method parameter of type String"
}这是我的请求
axios
.post(tokenUrl, credentials, {
headers: { "content-type": "application/json" }
})
.then(function(response) {
const clientToken = response.data;
console.log(clientToken);
console.log("fetched token via api");
//Get Receipt data
const receiptUrl =
"http://rest/financial/receipt";
const dateRange = {
params: {
from: "2020-03-12",
to: "2020-03-13"
}
};
const header = {
headers: {
"X-Auth-Token": clientToken,
"content-type": "application/json"
}
};
axios
.get(receiptUrl, dateRange, header)
.then(function(response) {
console.log(response.data);
console.log("fetched receipts via api");
})
.catch(function(error) {
console.log(error);
});
})
.catch(function(error) {
console.log(error);
});发布于 2020-03-13 10:11:27
你可以尝试这样的方法:
// Other code...
let data = {
params: {
from: "2020-03-12",
to: "2020-03-13"
},
headers: {
"X-Auth-Token": clientToken,
"content-type": "application/json"
}
};
axios.get(receiptUrl, data).then(function(response) {
// ...
}).catch(function(error) {
//...
});检查到达方法签名,即:
axios.get(URL, { params:{}, headers: { key: 'value' } })发布于 2020-03-13 10:22:05
如果令牌存在,尝试在默认标头中设置令牌。
if (clientToken) {
axios.defaults.headers.common["x-auth-token"] = clientToken;
} else {
delete axios.defaults.headers.common["x-auth-token"];
}
const header = {
headers: {
"content-type": "application/json"
}
};发布于 2020-03-13 10:02:59
您可以全局设置clientToken:
const JWT_TOKEN = 'jwt_access_token';
const api = axios.create({
baseURL: apiURL,
timeout: 5 * 60 * 1000,
});
if (localStorage.getItem(JWT_TOKEN)) {
const token = localStorage.getItem(JWT_TOKEN);
api.defaults.headers.common.Authorization = `Bearer ${clientToken}`;
}https://stackoverflow.com/questions/60668188
复制相似问题