【发布时间】:2017-02-27 04:45:46
【问题描述】:
https://www.npmjs.com/package/node-fetch 节点 v6.4.0 npm v3.10.3
我想在此 API 调用中发送带有自定义标头的 GET 请求。
const fetch = require('node-fetch')
var server = 'https://example.net/information_submitted/'
var loginInformation = {
username: "example@example.com",
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)
})
标题不适用,我被拒绝访问。 但是在 curl 命令中,它可以完美运行。
【问题讨论】:
-
您是否尝试过使用小写的标题名称? fetch 规范建议你这样做:https://fetch.spec.whatwg.org/#terminology-headers 另外:试试这个 curl - fetch 转换器工具:https://kigiri.github.io/fetch/
-
@Herald,
cURL -> Fetch您建议的转换器功能很差。只有标题被传递。其他所有内容,任何其他cURL选项都将被忽略。