Azure Functions: How to get data from a POST body in JavaScript

When working with Azure Functions, a common thing you'll want to do is get data out of the POST body inside your function. Handily, if you make your POST request in JSON format, you'll be able to simply access the object properties by name, directly on the request.body. For example, here's a simple Azure Function:

module.exports = async function (context, req) {
    if (req.body && req.body.name) {
        context.res = {
            body: "Hello " + req.body.name
        };
    } else {
        context.res = {
            status: 400,
            body: "Please pass a name in the request body"
        };
    }
};

The function expects a POST request with a name in the body and then returns "Hello ".

To be able to invoke this function and access properties directly on req.body, make sure you (1) make a POST to the endpoint (2) ensure the request content-type header is "application/json" and (3) ensure the JSON in the POST body is well-formed. Here's an example making such a POST request from Postman.

Postman Azure Function call

This is possible thanks to some underlying code in the Azure Functions internals which you can see in action here.