失眠生成的请求代码不起作用
我正在尝试向使用 cookie 的服务器发送登录请求。该请求在 Insomnia 客户端中完美运行。我的请求如下所示:
POST: https://website.com/...
Headers:
* accept: application/json, text/plain, */*
* accept-language: fr,fr-FR;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,hr;q=0.5
* content-type: application/json
* [custom-header-field]: jk#Bea201
Body is json:
{ "email": "...", "password": "..." }
当我发送请求时,它会起作用。返回带有响应数据 + set-cookie 标头的 200。 然而,失眠生成的代码不起作用。如下:
const fetch = require('node-fetch');
let url = '...';
let options = {
method: 'POST',
headers: {
accept: 'application/json, text/plain, */*',
'accept-language': 'fr,fr-FR;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,hr;q=0.5',
'content-type': 'application/json',
'custom-field-...': 'jk#Bea201'
},
body: '{"email":"...","password":"..."}'
};
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
由于某种原因,运行此代码使我的节点程序挂起,没有错误消息。 仅当我将从 Insomnia 请求收到的 cookie 添加到标头中时才起作用。但据我了解,我无法提前知道这些 cookie 是什么。 我什至尝试使用 fetch-cookie ,它给出了相同的结果。使用curl执行此操作会出现以下错误:
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>
You don't have permission to access "http://[website];com/authentication/login" on this server.<P>
Reference #18.44c01160.1645662994.95d786
</BODY>
</HTML>
这是因为CORS吗?
I'm trying to send a login request to a server that uses cookies. The request works perfectly in Insomnia client. My request looks like this:
POST: https://website.com/...
Headers:
* accept: application/json, text/plain, */*
* accept-language: fr,fr-FR;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,hr;q=0.5
* content-type: application/json
* [custom-header-field]: jk#Bea201
Body is json:
{ "email": "...", "password": "..." }
When I send the request it works. Returns a 200 with response data + set-cookie headers.
However, the code generated by insomnia doesn't work. Here it is :
const fetch = require('node-fetch');
let url = '...';
let options = {
method: 'POST',
headers: {
accept: 'application/json, text/plain, */*',
'accept-language': 'fr,fr-FR;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,hr;q=0.5',
'content-type': 'application/json',
'custom-field-...': 'jk#Bea201'
},
body: '{"email":"...","password":"..."}'
};
fetch(url, options)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
For some reason, running this code makes my node program hang, no error message. It only works when I add the cookies that I receive from the Insomnia request into the headers. But to my understanding, there is no way of me knowing in advance what those cookies are.
I have even tried using fetch-cookie, and it gives the same result. Doing it with curl gives me the following error:
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD><BODY>
<H1>Access Denied</H1>
You don't have permission to access "http://[website];com/authentication/login" on this server.<P>
Reference #18.44c01160.1645662994.95d786
</BODY>
</HTML>
Is this because of CORS?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论