meds
meds

Reputation: 22926

Can't see value of request headers in firebase?

I do the following:

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

On the firebase end:

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

Not sure how to get the header values, I just have the headers...

Upvotes: 4

Views: 8231

Answers (5)

Lewis Donovan
Lewis Donovan

Reputation: 1127

The Node docs tell us that rawHeaders is a list of headers, exactly as they were received.

The keys and values are in the same list. It is not a list of tuples. So, the even-numbered offsets are key values, and the odd-numbered offsets are the associated values.

So any index in this list that is an even number will be the name of a header, and any odd-number index will be a value. This means we can convert the rawHeaders array into a more useful array of key-value pairs like so:

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

This will leave us with just an array of key-value pairs like:

[
  {
    "header-name": "value"
  }, 
  ...
]

If you're just looking for one specific header, you could alternatively use the following:

// 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];

Upvotes: 0

user9168386
user9168386

Reputation:

If someone still has the same problem this is the solution for typscript:

To solve the CORS Problem first:

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
    });
});

Does not work:

console.error(request.headers.authorization);

Does Work:

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

Required Dependencies:

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

Upvotes: 1

Tod Birdsall
Tod Birdsall

Reputation: 19015

I found this answer while searching for "firebase request header is undefined".

Problem

I have written a firebase function (using Node JS) that I am posting to. The post included a header entitled "user_token". In my code, I retrieved the header value like this:

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

I tested this code on my local machine by using the Firebase CLI server (firebase serve) and it worked fine.

However, once I deployed this code (firebase deploy) to my Firebase account, I ran into a problem. Posting the exact same header key/value to my Firbase URL, I was getting errors because the userToken value was undefined.

Solution

Ultimately, changing my code to the following solved my problem:

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

@Ben's answer helped point me in the right direction. Initially, I simply changed my code to:

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

Unfortunately, I still ended up with an undefined userToken value. Looking over the Parsing HTTP requests documentation, I noticed that their sample header value key began with "x-". Updating my header value key from "user_token" to "x-user-token" did the trick.

Upvotes: 3

meds
meds

Reputation: 22926

Looks like the issue was with cors, so moving the body of the function into it worked, like so:

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

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

And then the cookies started appearing correctly...

I'm not entirely sure why though, this isn't a response thing it's just a request.

Upvotes: 4

Ben
Ben

Reputation: 5129

You should check Parsing HTTP requests.

You could simply get the request header by this method:

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

Upvotes: 3

Related Questions