【发布时间】:2022-01-21 12:30:45
【问题描述】:
我正在浏览器中使用 Rest 和 fetch() API 来发布带有 x-api-key 标头的数据以进行授权。
我用 Postman 和 curl 测试了 API,它工作得很好:
curl --header "X-API-KEY: my-api-key" -d "my-request-body" -X POST http://localhost:8081/submit
但是,它不适用于 fetch api。在 Chrome 上:
const response = await fetch(url, {
method: 'POST',
mode: 'no-cors',
cache: 'no-cache',
headers: {
'x-api-key': 'my-api-key',
'Accept': 'text/plain',
'Content-Type': 'text/plain'
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: 'my-request-body'
});
我无法从请求中检索 API 密钥。在我的快递中,路由器处理程序:
router.post('/submit', cors(), async (req,res, next) => {
const apiKey = req.header('x-api-key');
console.log(apiKey) // THIS GIVES undefined
});
req.headers 对象中也没有 x-api-key 标头。请帮助
【问题讨论】:
标签: node.js rest express fetch