简体   繁体   English

对外部 API 的相同请求适用于 go、postman、curl,但不适用于 node.js

[英]The same request to external API works in go, postman, curl, but doesn't work in node.js

I have a strange problem while performing request to external API.我在执行对外部 API 的请求时遇到了一个奇怪的问题。 Basically I got 403 error response for the HTTP request, but... only when calling it from node environment.基本上我得到了 HTTP 请求的403错误响应,但是......只有在从节点环境调用它时。 I tried with curl through terminal and it's OK.我尝试通过终端卷曲,没关系。 I also tried from Postman and native go with net/http package and everything is just fine, I got 200 response.我还尝试了 Postman 和 native go with net/http包,一切都很好,我得到了200响应。 But no matter what tool I use in node.js (request, axios, unirest, got) I can't make it.但是无论我在 node.js 中使用什么工具(request、axios、unirest、got)我都做不到。 I would love to share entire request with you, but unfortunately I can't :(我很想与您分享整个请求,但不幸的是我不能:(

All I can show you is some response details, when I call it from node.js environment.当我从 node.js 环境中调用它时,我只能向您展示一些响应详细信息。

These are headers sent by the server when I got 403 :这些是我收到403时服务器发送的标头:

  headers: {
    server: 'AkamaiNetStorage',
    'content-length': '228',
    'content-type': 'application/json',
    'x-edge-error': 'halt',
    'cache-control': 'max-age=122',
    date: 'Thu, 21 Oct 2021 16:17:41 GMT',
    connection: 'close'
  },

And the part of response data looks like this:响应数据部分如下所示:

  data: {
    edge_error: 'halt',
    ref_id: '18.9d6656b8.1634833061.4b9f14b',
  }

I am aware that question is strange and you probably won't be able to help me with not so many details, but I will try anyway.我知道这个问题很奇怪,您可能无法在没有太多细节的情况下帮助我,但无论如何我都会尝试。 So If anyone have idea what might help, feel free to propose an answer.因此,如果有人知道什么可能会有所帮助,请随时提出答案。

Best regards ;)最好的祝福 ;)

This is likely due to TLS fingerprinting.这可能是由于 TLS 指纹识别造成的。 Akamai is able to detect that you're using Node.JS based on information gathered during the TLS handshake (cipher suites, etc.) when you send a request.当您发送请求时,Akamai 能够根据在 TLS 握手期间收集的信息(密码套件等)检测您正在使用 Node.JS。 The website must have set a rule through Akamai to block Node.该网站必须通过 Akamai 设置规则来阻止 Node.js。

Try something like this:尝试这样的事情:

const tls = require('tls');
const https = require('https');

const defaultCiphers = tls.DEFAULT_CIPHERS.split(':');
const shuffledCiphers = [
    defaultCiphers[0],
    // Swap the 2nd & 3rd ciphers:
    defaultCiphers[2],
    defaultCiphers[1],
    ...defaultCiphers.slice(3)
].join(':');

request = require('https').get('https://google.com', {
    ciphers: shuffledCiphers
}).on('response', (res) => {
    console.log(res.statusCode);
});
// source: https://httptoolkit.tech/blog/tls-fingerprinting-node-js/

More information about this can be found here: https://httptoolkit.tech/blog/tls-fingerprinting-node-js/更多信息可以在这里找到: https : //httptoolkit.tech/blog/tls-fingerprinting-node-js/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM