You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function无法获取请求体问题求助

.NET 8 Azure Function App POST请求体为空/Null的解决方法

问题描述

我创建了一个示例.NET 8 Function App并部署至Azure,通过POST请求发送数据调用函数时,函数能正常触发,但请求体始终返回为空/Null。以下是我的函数代码及local.settings.json配置:

函数代码

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

local.settings.json配置

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

解决方案

  • 确保请求携带正确的Content-Type头
    POST请求必须设置Content-Type: application/json,否则Azure Functions无法正确识别JSON格式的请求体,导致req.Body读取为空。

  • 改用模型绑定简化请求体处理
    .NET 8支持直接将请求体绑定到强类型模型,避免手动操作流的潜在问题。修改后的代码示例:

    public class NameRequest
    {
        public string Name { get; set; }
    }
    
    public static class Function1
    {
        [FunctionName("Test")]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [FromBody] NameRequest requestModel,
            ILogger log)
        {
            string name = req.Query["name"] ?? requestModel?.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);
        }
    }
    
  • 重置流位置后再读取
    如果坚持手动读取流,需确保流的指针回到起始位置,避免因流已被读取过导致空值:

    req.Body.Position = 0; // 添加这行重置流位置
    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    
  • 检查请求体格式是否匹配
    如果使用Postman等工具测试,确认请求体是raw格式的JSON内容,而非form-data或x-www-form-urlencoded。如果是表单格式,需要通过req.Form["name"]获取参数,而非读取req.Body。

  • 确认部署后的应用配置一致
    检查Azure门户中Function App的应用设置,确保FUNCTIONS_WORKER_RUNTIME为dotnet、FUNCTIONS_INPROC_NET8_ENABLED为1,与本地配置一致。

内容的提问来源于stack exchange,提问作者Balanjaneyulu K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:09:50