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

AWS C# Lambda接收POST JSON返回空数组问题排查

问题

通过Postman或curl向AWS C# Lambda发送POST请求传递JSON对象,期望Lambda读取并返回属性值,但实际返回空数组。相关代码及请求信息如下:

Lambda代码

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Newtonsoft.Json;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace LambdaS3FileUpload;

public class Function
{
    // public async Task<APIGatewayProxyResponse> FunctionHandler(APIGatewayProxyRequest request)
    public Object FunctionHandler(APIGatewayProxyRequest request)
    {
        var req = JsonConvert.DeserializeObject(request.Body);
        return req;
    }
}

curl请求

curl --location 'https://uniqueid.lambda-url.ap-southeast-2.on.aws/' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{"field_one":1, "field_two":2}'

错误返回

{"field_two":[],"field_one":[]}

解决方案

问题根源是序列化/反序列化库冲突以及返回类型处理不当:

  • Lambda配置的序列化器是SystemTextJson.DefaultLambdaJsonSerializer,但代码里却用Newtonsoft.Json(Json.NET)反序列化请求体,两者不兼容导致解析异常。
  • 直接返回JsonConvert.DeserializeObject生成的JObject(未指定类型时的默认返回),再经System.Text.Json二次序列化时,会把数值类型错误解析为空数组。

修复方案一:统一使用System.Text.Json

移除Newtonsoft.Json依赖,用System.Text.Json处理序列化:

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using System.Text.Json;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace LambdaS3FileUpload;

public class Function
{
    public object FunctionHandler(APIGatewayProxyRequest request)
    {
        var req = JsonSerializer.Deserialize<dynamic>(request.Body);
        return req;
    }
}

修复方案二:统一使用Newtonsoft.Json

若坚持用Newtonsoft.Json,需替换Lambda序列化器并调整代码:

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using Newtonsoft.Json;

// 替换为Newtonsoft对应的Lambda序列化器
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace LambdaS3FileUpload;

public class Function
{
    public object FunctionHandler(APIGatewayProxyRequest request)
    {
        var req = JsonConvert.DeserializeObject(request.Body);
        return req;
    }
}

更规范的类型安全做法:定义实体类

为避免动态解析的不确定性,建议定义匹配请求结构的实体类:

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using System.Text.Json;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace LambdaS3FileUpload;

// 定义与请求JSON结构匹配的实体类
public class RequestModel
{
    public int field_one { get; set; }
    public int field_two { get; set; }
}

public class Function
{
    public RequestModel FunctionHandler(APIGatewayProxyRequest request)
    {
        return JsonSerializer.Deserialize<RequestModel>(request.Body);
    }
}

内容的提问来源于stack exchange,提问作者Ela Buwa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 07:32:50