Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore Cloud Function - How to read params from url

I need to extract the params from http url in firestore cloud function,
HTTP URL

 https://us-central-xxxxxxxxxxx.cloudfunctions.net/somefunction?name=ABC&age=80

Firestore Cloud function

export const somefunction = functions.https.onRequest(async (request, response) => {
  var name = // need to extract name from request
  var age = // similarly need to extract age from request
}

Can anyone help me out with this ?

like image 728
Stack Avatar asked Sep 15 '25 10:09

Stack


2 Answers

You can use the request object to access the query parameters similar to what you do in Express Js

export const somefunction = functions.https.onRequest(async (request, response) => {
  var name = request.query.name
  var age = request.query.age
}

If the request was from a POST request then you can use request.body.* for accessing values.

like image 123
Shababb Karim Avatar answered Sep 18 '25 09:09

Shababb Karim


I have a similar issue where I cannot get req.url and req.query.abc as asked in Cannot get req.path and req.query.abc with firebase functions

As mentioned in the comments of Call Firebase cloud function via GET and return parameters this is happening only on development.

After searching for a long time I came across this article https://howtofirebase.com/cloud-functions-migrating-to-node-8-9640731a8acc where it said firebase-tools have to be of 4.0.0 version. I've updated my firebase-tools to 4.0.0 and I can get the query parameters normally

like image 30
Green Wizard Avatar answered Sep 18 '25 08:09

Green Wizard