Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Functions: "status":"INVALID_ARGUMENT"

I am locally testing a Firebase Cloud Function. When I call this function using the local URL http://localhost:5001/projectName/us-central1/functionName as described here:

exports.createSession = functions.https.onRequest((_req, res) => {
  res.status(200).send('TESTING');
});

the function works and it returns the string.

However, when I call this function:

exports.createSession = functions.https.onCall((data, context) => {
  return 'TESTING';
});

It throws the error: {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

I'd like to use the latter function because I want to access the context.auth object to check for user Firebase authentication.

I'm using Firebase CLI v8.4 and Node v10.20.

What am I missing from the second function to get it working? I am not calling it with any arguments as I do not need to.

like image 636
Mr. Robot Avatar asked Apr 16 '26 06:04

Mr. Robot


1 Answers

You're comparing a callable function using onCall to an HTTP function using onRequest. Callable functions are implemented very differently than HTTP functions. I suggest reading over the documentation I linked in order to better understand the difference. The main point is that callable functions follow a specific protocol, and that any client access must follow that protocol, or possibly receive the error that you show here. The best implementation of that protocol is the client SDK provided by Firebase - you should use that to invoke the function.

If you want a normal HTTP function to be invoked using the usual HTTP libraries, then don't use a callable at all. You can pass authentication information to an HTTP function manually using an ID token then verify it using the Firebase Admin SDK. The documentation link has examples.

like image 65
Doug Stevenson Avatar answered Apr 17 '26 19:04

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!