node.js 带有标头的节点获取 API GET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40099282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:45:58  来源:igfitidea点击:

Node-Fetch API GET with Headers

node.jsapi

提问by essxiv

https://www.npmjs.com/package/node-fetchNode v6.4.0 npm v3.10.3

https://www.npmjs.com/package/node-fetchNode v6.4.0 npm v3.10.3

I want to send a GET Request with custom headers in this API call.

我想在此 API 调用中发送带有自定义标头的 GET 请求。

const fetch = require('node-fetch')
var server = 'https://example.net/information_submitted/'

var loginInformation = {
    username: "[email protected]",
    password: "examplePassword",
    ApiKey: "0000-0000-00-000-0000-0"
}

var headers = {}
headers['This-Api-Header-Custom'] = {
    Username: loginInformation.username,
    Password: loginInformation.password,
    requiredApiKey: loginInformation.ApiKey
}

fetch(server, { method: 'GET', headers: headers})
.then((res) => {
    console.log(res)
    return res.json()
})
.then((json) => {
    console.log(json)
})

The headers are not applying, I am denied access. But within a curl command, it works perfectly.

标题不适用,我被拒绝访问。但是在 curl 命令中,它可以完美运行。

回答by Ivica Vucemilo

Let's use this bash command netcat -lp 8081and change the url temporarily to http://localhost:8081/testurl. Now, the request will still fail, but our console shows some raw request data:

让我们使用这个 bash 命令netcat -lp 8081并将 url 临时更改为http://localhost:8081/testurl. 现在,请求仍然会失败,但我们的控制台会显示一些原始请求数据:

user@host:~$ netcat -lp 8081
GET /testurl HTTP/1.1
accept-encoding: gzip,deflate
user-agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch)
connection: close
accept: */*
Host: localhost:8081\r\n
\r\n

The two \r\n's are n fact invisible CRLFs, the spec says, these mark the end of headers and the beginning of the request body. You can see the extra new line in your console. Now if you rather want it to look like this:

\r\n规范说,这两个实际上是不可见的 CRLF,它们标志着标头的结束和请求正文的开始。您可以在控制台中看到额外的新行。现在,如果您希望它看起来像这样:

user@host:~$ netcat -lp 8081
GET /testurl HTTP/1.1
username: [email protected]
password: examplePassword
requiredapikey: 0000-0000-00-000-0000-0
accept-encoding: gzip,deflate
user-agent: node-fetch/1.0 (+https://github.com/bitinn/node-fetch)
connection: close
accept: */*
Host: localhost:8081

then you only need a little change:

那么你只需要一点点改变:

// var headers = {}
//headers['This-Api-Header-Custom'] = {
var headers = {
  Username: loginInformation.username,
  Password: loginInformation.password,
  requiredApiKey: loginInformation.ApiKey
}

fetch(server, { method: 'GET', headers: headers})

But if you want to set some special header This-Api-Header-Custom, then you can't pass in nested objects and arrays, but you have to serialize your data, i.e. turn username/password/requiredApiKey data into a string. Depending on your requirements, that might be e.g. CSV, JSON, ...

但是如果你想设置一些特殊的 header This-Api-Header-Custom,那么你不能传入嵌套的对象和数组,而是你必须序列化你的数据,即将用户名/密码/requiredApiKey 数据转换为字符串。根据您的要求,这可能是例如 CSV、JSON、...

回答by newswim

I think you need to use the Headers constructor, instead of a plain object.

我认为您需要使用 Headers 构造函数,而不是普通对象。

https://developer.mozilla.org/en-US/docs/Web/API/Headers

https://developer.mozilla.org/en-US/docs/Web/API/Headers

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Headers

myHeaders = new Headers({
  "Content-Type": "text/plain",
  "Content-Length": content.length.toString(),
  "X-Custom-Header": "ProcessThisImmediately",
});