How to disable GCF URL query string parsing

Viewed 38

I have a simple node.js cloud function that I need the un-parsed original URL within. In the request object, we have request.url, request.originalURL but both of these seem to be parsed upstream of the function into key=parameter pairs. For example, calling the function from firebase hosting with the url https://www.example.com/index.html?var1&var2&var3 would yield an output of "/index.html?var1=&var2=&var3=" for either request attribute. I can remove the "=" characters, but it gets more complex. For example, if var1="123,456" I now have "/index.html?123%2C456=&var2=&var3=" which requires me to un-parse, and also decode the URL.

The real show stopper, is that for some reason var1 can end up after var2 and var3. I.E. "/index.html?var2&var3&var1". This doesn't happen with the local emulator, but does on the deployed version. Since I'm not using key=value pairs, I rely on the variables being in the same order. I can't change the structure without a lot of work on the front end app.

When I run the function locally with firebase emulator, i can use request.headers["x-original-url"] but this is not available when deployed to GCF.

In the express documentation, it shows that the query parser can be turned off by app.disable("query parser") or app.set("query parser", false), but neither of these have an effect on the url locally or in the cloud.

Here is a snippet to recreate the issue:

const functions = require("firebase-functions");
const express = require('express');
const app = express();

app.disable('query parser');
// also tried
// app.set('query parser', false);

app.get("*", async (req, res) => {

  functions.logger.log(req.url, req.originalUrl);
  return res.status(200).send(""); // 
});

exports.ssr = functions.https.onRequest(app);

I think either I'm missing something in the documentation, or there is a feature to disable the express parser missing from google cloud functions.

0 Answers
Related