Firebase functions onRequest to onCall

Viewed 40

How do I pass this onRequest function to onCall? I am working from my localhost with emulators. Could someone give me some guidance, I have tried to follow the documentation of functions.https.onCall but I can't understand if I have to do any previous step.

export const getFileInformation = functions.https.onRequest( (req, res) => {

  return cors( req, res, () => {

    const urls = [
      `url1`,
      `url2`,
      `url3`
    ];

    const urlsCalls: any[] = [];
    const resultados: any[] = [];

    urls.forEach( url => {
      urlsCalls.push(axios.get(url));
    });

    Promise.allSettled(urlsCalls)
    .then( response => {
      response.map( (element: any) => {
        const item = element.value.data;
        resultados.push(item);
      });
      console.log(resultados);
      res.json(resultados);
    })
    .catch( error => {
      console.log(error);
    });
  } );
});

I'm trying something as simple as this:

export const getFileInformation2 = functions.https.onCall( (data, context) => {
  return { msg: 'Hello from Firebase!' };
});

But I get the following error:

{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

How should I address an onCall function?

1 Answers

HTTPS Callable functions must be called using the POST method, the Content-Type must be application/json, and the body must contain a field called data for the data to be passed to the method.

See sample body:

{
    "data": {
        "someKey": "someValue"
    }
}

When pasting the URL directly to a browser it is called using the GET method which returns an error Request has invalid method. GET and the reason you're getting {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}} is you're not passing the required field which is data on your request body.

You can call a https.onCall using curl or Postman. Sample call using curl:

curl -d '{"data": {"someKey": "someValue"}}' -H 'Content-Type: application/json' http://localhost:5001/[myProjectName]/us-central1/getFileInformation2

For more information, you may check :

Related