Search code examples
firebasegoogle-cloud-functionsfirebase-tools

Why firebase emulator function says request body is missing data?


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?


Solution

  • 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: