[SOLVED] Firebase functions onRequest to onCall

Issue

This Content is from Stack Overflow. Question asked by Paco Zevallos

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);
    });
  } );
});



Solution

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 :


This Question was asked in StackOverflow by Paco Zevallos and Answered by Marc Anthony B It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?