1

I'm using firebase serverless function to store my lead,

I'm under impression that firebase use request object from express.js, so here's my code:

export const addLead = functions
  .https.onRequest(async (req, res) => {
    const data = {
      name: req.body.name,
      phoneNumber: req.body.phoneNumber,
      email: req.body.email,
      path: req.body.path,
    };

    console.log(
      JSON.stringify({
        headers: req.headers,
        method: req.method,
        url: req.url,
        httpVersion: req.httpVersion,
        body: req.body,
        cookies: req.cookies,
        path: req.path,
        protocol: req.protocol,
        query: req.query,
        hostname: req.hostname,
        ip: req.ip,
        originalUrl: req.originalUrl,
        params: req.params,
      })
    );

    const docRef = await db.collection("leads").add(data);

    // ... omitted

  });

Here's the result of the console log in jsonviewer:

all url is empty

How to get the req.originalUrl, for ex: /services/renovation?q=houserenovation.

Or

How do I get the fullURL of the request is coming from? for ex: http://localhost:3000/services/renovation?q=houserenovation

It has to be http function because I don't want to install javascript on the client, it's submitted through form.

I already tried in staging server, it's still showing empty "/"

Kevin Tanudjaja
  • 866
  • 1
  • 9
  • 16
  • 1
    To be honest, that result doesn't look correct for the request that you stated in the question. Maybe you should edit the question to show exactly what you did to generate that result. We should be able to duplicate the result using the instructions you provide. – Doug Stevenson May 22 '23 at 01:05

1 Answers1

0

To get the fullURL of the request is coming from, you may try like this:

export const helloWorld = functions.https.onRequest((req, res) => {
       const fullUrl = req.protocol + '://' + req.get('Origin') + req.originalUrl;
       console.log(fullUrl);
       res.status(200).send(fullUrl);
});

Also you can check with this related Stackoverflow Link, which might be helpful.

  • 1
    why my req.originalUrl is "/" ? – Kevin Tanudjaja May 22 '23 at 07:17
  • Because your onRequest firebase function only have 1 url which is `/` but if you configure this with express app like shown in the [Documentation](https://firebase.google.com/docs/hosting/functions#use_a_web_framework) then if the request comes from `/api` route(check point 3b) then the originalURL will be `/api`. – Sandeep Vokkareni May 23 '23 at 06:20
  • 1
    I'm pretty sure my request is from a static page (html,css, no js): http://localhost:3000/services/renovation?q=houserenovation it doesn't detect the originalUrl – Kevin Tanudjaja May 23 '23 at 11:20
  • It doesn’t matter if you have a different setup. As @Doug Stevenson mentioned in the question, you may edit your question or raise a new case by sharing code or what exactly you did. – Sandeep Vokkareni May 24 '23 at 07:38