在 firebase 中看不到请求标头的值?

问题描述 投票:0回答:5

我执行以下操作:

var headers = new Headers();
headers.append("bunny", "test");
headers.append("rabbit", "jump");
fetch("blahurl.com/someservice", {headers:headers})

在火力基地端:

export const listener = functions.https.onRequest(async (req, res) => {
 for (let entry of req.rawHeaders) {
        console.log(entry); // here I see a 'bunny,rabbit' get printed somewhere
    }

     //However this yields undefined:
     req.headers.bunny

不确定如何获取标题值,我只有标题...

node.js typescript firebase http-headers google-cloud-functions
5个回答
3
投票

您应该检查解析 HTTP 请求

您可以通过此方法简单地获取请求头:

req.get('bunny');
req.get('rabbit');

3
投票

看起来问题出在 cors 上,因此将函数主体移入其中是有效的,如下所示:

const cors = require('cors')({ origin: true });

export const listener = functions.https.onRequest(async (req, res) => {
    cors(req, res, () => {

然后cookies开始正确显示......

我不完全确定为什么,这不是一个响应,而只是一个请求。


2
投票

我在搜索“firebase 请求标头未定义”时找到了这个答案。

问题

我已经编写了一个 firebase 函数(使用 Node JS),并将其发布到其中。该帖子包含标题为“user_token”的标题。在我的代码中,我检索了如下标头值:

const userToken = request.header("user_token");

我使用 Firebase CLI 服务器 (

firebase serve
) 在本地计算机上测试了此代码,并且运行良好。

但是,当我将此代码 (

firebase deploy
) 部署到我的 Firebase 帐户后,我遇到了问题。将完全相同的标头键/值发布到我的 Firbase URL,我收到错误,因为
userToken
值是
undefined

解决方案

最终,将我的代码更改为以下内容解决了我的问题:

const userToken = request.get("x-user-token");

@Ben 的回答帮助我指明了正确的方向。最初,我只是将代码更改为:

const userToken = request.get("user_token");

不幸的是,我仍然得到了一个未定义的 userToken 值。查看解析 HTTP 请求文档,我注意到他们的示例标头值键以“x-”开头。将我的标头值键从“user_token”更新为“x-user-token”就成功了。


1
投票

如果有人仍然遇到同样的问题,这是typscript的解决方案:

首先解决CORS问题:

import * as cors from 'cors';
const corsHandler = cors({origin: true});

export const getmBucksShop = functions.https.onRequest((request, response) => {
    corsHandler(request, response, () => {
        //Access Header Values here
        //Your Code Here
    });
});

不起作用

console.error(request.headers.authorization);

有效吗

console.error(request.headers['authorization']);
console.error(request.get("authorization"));

所需的依赖项:

    "dependencies": {
    "cookie-parser": "^1.4.3",
    "cors": "^2.8.5",
    "express": "^4.16.4",
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.1.0"
  },

0
投票

Node 文档告诉我们 rawHeaders 是标头列表,与收到的标头完全相同。

键和值位于同一个列表中。它不是元组列表。因此,偶数偏移量是键值,奇数偏移量是关联值。

因此,该列表中的任何偶数索引都将是标头的名称,而任何奇数索引都将是一个值。这意味着我们可以将 rawHeaders 数组转换为更有用的键值对数组,如下所示:

const headers = req.rawRequest.rawHeaders.map((h, i) => { // Bail if the index is odd if(i%2 !== 0) return false; // Create a blank object const rtn = {}; // Add the current rawHeader as the key, // and the next rawHeader as the value rtn[h] = req.rawRequest.rawHeaders[i+1]; return rtn }).filter(Boolean); // Filter out the odd values we skipped
这将使我们只剩下一个键值对数组,例如:

[ { "header-name": "value" }, ... ]
如果您只是寻找一个特定的标题,您也可以使用以下内容:

// Get the index of the specific header we're looking for const bunnyIndex = req.rawRequest.rawHeaders.findIndex((h) => { return h === "bunny"; }); // Get the value by simply adding 1 to the index const bunny = req.rawRequest.rawHeaders[bunnyIndex + 1];
    
© www.soinside.com 2019 - 2024. All rights reserved.