[英]API request works with postman, but not in Node.js (request)
I am working on a project that talks with an API, my script is in nodeJS (it's a big project so I will give you a very simplified version)我正在开发一个与 API 对话的项目,我的脚本在 nodeJS 中(这是一个大项目,所以我会给你一个非常简化的版本)
Essentially when I do a PUT request to the API, with the postman, it works fine, When I do it with my Code: GET works great (so I know the URL is correct), but PUT doesn't work with my script, I get no errors on my console, and the API simple sends back the OK status, so I have no clues where it went wrong基本上,当我使用邮递员向 API 发出 PUT 请求时,它工作正常,当我使用我的代码执行此操作时:GET 效果很好(所以我知道 URL 是正确的),但是 PUT 不适用于我的脚本,我的控制台上没有错误,API simple 发回 OK 状态,所以我不知道哪里出错了
Here is a very basic version of my script (that doesn't work either)这是我的脚本的一个非常基本的版本(也不起作用)
(I know the URL is correct) (我知道网址是正确的)
PUT parameters: PUT参数:
{ method: 'PUT',
baseUrl: 'https://fusionrv.XXXX.XX/fusion/apiservice',
url:
'XXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXX',
qs:
{ auth:
'AUTH_KEY',
value: 1 },
headers: { 'content-type': 'application/json' } }
simple code (without the imports):简单代码(没有导入):
request(putParams, (err, res) => {
if (err) console.log(err);
data = JSON.parse(res.body)
console.log(data)
});
In Postman, I have the same baseURL and url, as well as the same params "auth" and "value", the body is set to "none", and headers are Content-type: application/json
.在 Postman 中,我有相同的 baseURL 和 url,以及相同的参数“auth”和“value”,正文设置为“none”,标题为
Content-type: application/json
。
The response (both in Postman and NodeJS):响应(在 Postman 和 NodeJS 中):
{
"Status": "Success"
}
So does anyone know why it works with postman but not with the request module?那么有谁知道为什么它适用于邮递员而不适用于请求模块? I appreciate all of you!
我感谢你们所有人!
原来问题出在API上,我需要执行POST请求并使用其他URL来访问它,这是一个非常奇怪的API。
添加用户代理作为标题的一部分
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.