Unable to readrequest body in Azure function

Balanjaneyulu Kantu (Quadrant Resource LLC) 0 Reputation points Microsoft External Staff
2025-02-11T06:09:03.02+00:00

I have created a sample .NET 8 function app and deployed in Azure. I tried to invoke by sending some data in the post.

The function is invoked but body is sending as null/ empty.

Host File

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            },
            "enableLiveMetricsFilters": true
        }
    }
}

Local.Settings.Json

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_INPROC_NET8_ENABLED": "1",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet"
    }
}

Function

public static class Function1
{
    [FunctionName("Test")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        log.LogInformation($" RequestBody: {requestBody}");

        string name = req.Query["name"];

        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;

        string responseMessage = string.IsNullOrEmpty(name)
            ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
            : $"Hello, {name}. This HTTP triggered function executed successfully.";

        return new OkObjectResult(responseMessage);
    }
}
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,569 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ranashekar Guda 970 Reputation points Microsoft External Staff
    2025-02-12T12:18:55.89+00:00

    Hi @Balanjaneyulu Kantu (Quadrant Resource LLC),
    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.
    It looks like you're having trouble reading the request body in your Azure Function. Here are a few things to check:

    1. Configuration: Ensure your function is properly configured to read the request body. If you're using HttpRequestData, make sure you're accessing the body correctly.
    2. Content Type: Verify that the content type of the request is set correctly. For JSON data, it should be application/json. If the content type is missing or incorrect, the body may not be read properly.
    3. Asynchronous Operation: If you're using the HttpRequestData object, remember that reading the body is an asynchronous operation. Make sure to await the read operation.
    4. Local Testing: If you're testing locally, check that your local settings and environment are configured correctly. The local.settings.json file should contain the necessary settings for your function to operate properly.
    5. Logging: Add logging to your function to output the headers and any other relevant information. This can help diagnose why the body is coming through as null or empty.

    For more detailed information, you can refer to the following documentation:

    Guide for running C# Azure Functions in the isolated worker model
    Quickstart: Create and deploy functions to Azure Functions using the Azure Developer CLI

    If you find the answer helpful, kindly click "Accept Answer" and upvote it. If you have any further questions or concerns, please feel free to reach out to us. We are happy to assist you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.