gpt4 book ai didi

javascript - axios删除方法给出403

转载 作者:行者123 更新时间:2023-12-03 01:19:13 26 4
gpt4 key购买 nike

我正在从我的 Node-js 应用程序调用删除方法。

Its working fine from Postman but giving me 403 while calling this API from code.

下面是我的示例代码片段:

const instance = axios.create();
instance.interceptors.request.use((config) => {
config.baseURL = 'https://test-dev.com/api/portfolio'
config.headers = { 'Authorization' : 'Bearer ' + <TOKEN>}
return config;
});
instance.delete('/admin?users=<VALUE>').then(function(response) {
console.log("Deleted: "+<VALUE>);
}).catch(function (error) {
console.log("Deletion failed with error:" + error);
});

编辑:

响应(来自spring security APP):

Could not verify the provided CSRF token because your session was not found

我认为这已经由 axios 处理了。

如何在调用删除方法时在 header 中传递此值?

有什么帮助吗?

最佳答案

您可以:

1 - 使用 withCredentials 属性:

withCredentials: true

所以:

axios.delete({
url: 'https://test-dev.com/api/portfolio/admin?users=' + <VALUE>,
headers: { 'Authorization' : 'Bearer ' + <TOKEN>},
withCredentials: true
}).then(function(response) {
console.log("Deleted: "+<VALUE>);
}).catch(function (error) {
console.log("Deletion failed with error:" + error);
});

The XMLHttpRequest.withCredentials property is a Boolean that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates. Setting withCredentials has no effect on same-site requests.

2 - 设置 CSRF header

或者:

headers: {'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content')}

headers: {'X-Requested-With': 'XMLHttpRequest',
'X-CSRFToken': 'your token here'}

或者只是:

headers: {'X-Requested-With': 'XMLHttpRequest'}

3 - 如果可能,请自行承担禁用风险

看看this article

关于javascript - axios删除方法给出403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51836854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com