javascript - 如何修复 CSRF 不匹配?
问题描述
我正在尝试使用 expressJS 通过我的 js 应用程序中的“axios”包运行对我正在使用的 API 的请求。
尽管令牌正确,但我只是不断收到“CSRF 令牌不匹配”,它使用 YARC google chrome 扩展程序工作,但在我的应用程序或邮递员中不起作用。
我正在使用的代码是:
var options = {
"method": "POST",
"url": "https://example.com/lines/data",
"headers": {
"X-CSRF-TOKEN": "IZ1QTMDHl3D0HFkAPemC13 - removed some of token",
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8"
},
data: {
'token': 'IZ1QTMDHl3D0HFkAPemC13 - removed some of token',
}
}
request(options, function(err, response, body) {
if(err) {
if(err.code == "ETIMEDOUT" && err.connect == true) {
res.status(500).json({status: "FAILED"})
return;
} else {
console.log(err);
res.status(500).json({status: "FAILED"})
return;
}
}
// var body = JSON.parse(body);
res.send(body);
})
它返回 "{ "message": "CSRF token mismatch." }"
使用带有 chrome 扩展的以下请求(https://chrome.google.com/webstore/detail/yet-another-rest-client/ehafadccdcdedbhcbddihehiodgcddpl?hl=en)工作正常并返回我的 json
{
"method": "POST",
"transformRequest": [
null
],
"transformResponse": [
null
],
"url": "https://example.com/lines/data",
"headers": {
"X-CSRF-TOKEN": "IZ1QTMDHl3D0HFkAPemC13 - removed some of token",
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=utf-8"
},
"data": "",
"timeout": {}
}
- 注意:我对所有这些都是新手,所以如果它真的很基本,我很抱歉。
解决方案
推荐阅读
- javascript - JS 对象的索引返回对象中的所有值而不是实际的索引
- r - Ggplot,用 3 行标签注释绘图,里面带有粗体文本
- java - 在 cousera 的 Algorithms I 中,老师使用哪个终端命令行?
- javascript - 重复 XMLHttpRequest
- json - Haskell - Aeson:尝试解码 JSON URL Req 时得到“无”
- excel - 来自powershell的excel数据类型不匹配
- angular - 在 Angular 6 中过滤用户而不创建自定义管道
- asp.net-core - 如何在.Net Core Api Server的FromForm情况下使用自定义绑定来绑定属性
- php - TrustProxies 和 Laravel 返回我的服务器的 IP 地址,而不是最终用户
- python - 如何在selenium webdriver python3.7中使用带有内部html的find_element_by_xpath单击按钮