失眠生成的请求代码不起作用

发布于 2025-01-09 08:29:11 字数 1650 浏览 0 评论 0原文

我正在尝试向使用 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&#58;&#47;&#47;[website];com&#47;authentication&#47;login" on this server.<P>
Reference&#32;&#35;18&#46;44c01160&#46;1645662994&#46;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文