3

I'm running my local emulator suite and I'm constantly getting error messages for my curl requests. The following command:

curl -X POST http://localhost:5001/my-project/us-central1/myFunction \
    -H "Content-Type: application/json" \
    -d '{"productId": 123456, "quantity": 100}'  

Is always showing this in the emulator CLI:

>  {"productId":123456,"quantity":100,"severity":"WARNING","message":"Request body is missing data."}
>  {"severity":"ERROR","message":"Invalid request, unable to process."}

None of the code was executed in the function as it starts with a console log which is never printed here. Any thoughts?

Mr.Drew
  • 939
  • 2
  • 9
  • 30

1 Answers1

6

That error occurs when you use the onCall method. So, I would assume that you are using functions.https.onCall. As explained in this documentation:

It's important to keep in mind that HTTPS callable functions are similar but not identical to HTTP functions. To use HTTPS callable functions you must use the client SDK for your platform together with the functions.https backend API (or implement the protocol).

If you want to directly call the function via its endpoint then you should follow the protocol specification for https.onCall. One of its requirements is the request body should be a JSON object with data as its main key.

An example request JSON object should be like this instead:

{
    "data": {
        "productId": 123456,
        "quantity": 100
    }
}

For reference, here's the full curl request:

curl -X POST http://localhost:5001/my-project/us-central1/myFunction \
    -H "Content-Type: application/json" \
    -d '{ "data": { "productId": 123456, "quantity": 100 } }' 

For more information, you may check out this documentation:

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19
  • I solved my issue using onRequest() instead of onCall(), but I'll try your suggestion with onCall() and if it works will mark the answer as correct. Thank you. – Mr.Drew Jun 11 '22 at 00:10
  • Yes. using `onRequest()` will accept your JSON object as it does not require data as its main key. – Marc Anthony B Jun 11 '22 at 02:07