让我们使用这个 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
事实上,规范说,它们是不可见的 CRLF,它们标记标头的结束和请求正文的开始。您可以在控制台中看到额外的新行。
现在,如果您希望它看起来像这样:
user@host:~$ netcat -lp 8081
GET /testurl HTTP/1.1
username: [email protected] /cdn-cgi/l/email-protection
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
那么你只需要一点点改变:
// var headers = {}
//headers['This-Api-Header-Custom'] = {
var headers = {
Username: loginInformation.username,
Password: loginInformation.password,
requiredApiKey: loginInformation.ApiKey
}
fetch(server, { method: 'GET', headers: headers})
但是如果你想设置一些特殊的标题This-Api-Header-Custom
,那么你不能传入嵌套的对象和数组,但你必须序列化你的数据,即将用户名/密码/requiredApiKey数据转换为字符串。根据您的要求,这可能是例如CSV、JSON、...