如何使用AWS Lambda模拟工具来解析JSON数组。

huangapple go评论65阅读模式
英文:

.net core How to get AWS lambda mock tool to parse json array

问题

你的代码是在将.NET Core 6的多个Web服务转换为AWS Lambda函数。到目前为止,我已经完成了八个,没有任何问题,除了这个问题。我正在尝试使用模拟Lambda工具从Visual Studio进行测试。这是我的模拟JSON请求:

{
  "body": "{\"PolicyNumbers\":[{\"Number\":\"WCU9799P\"},{\"Number\":\"B1V48603\"}],\"RenewalEffectiveDate\":\"09/28/2016\",\"State\":\"\"}",
  "resource": "/{proxy+}",
  "path": "api/GetClaimsCount",
  "httpMethod": "POST",
  "headers": {
    "Accept": "application/json",
    "Accept-Encoding": "gzip, deflate, sdch",
    "Accept-Language": "en-US,en;q=0.8"
  }
}

在调试器命中我的函数处理程序的第一行之前,我收到了一个以以下内容开始的长错误:

System.Exception: Error deserializing the input JSON to type String
   at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in ...

以下是我的函数处理程序的开头部分。我在其他Lambda函数上使用相同的代码,它可以正常工作。我认为这与JSON的数组部分有关。如果我是对的,那么如何格式化我的请求以接受JSON数组?

public Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(string input, ILambdaContext context)
{
    string? message = "";
    string inputString = (input != null) ? JsonSerializer.Serialize(input) : string.Empty;
    _log.Debug("Request:" + inputString);
}
英文:

I am converting several .NET Core 6 web services to AWS Lambda functions. I have completed eight so far with no issues, except for this one. I am trying to test it from Visual Studio using the mock lambda tool. This is my mock json request:

{
  &quot;body&quot;: &quot;{\&quot;PolicyNumbers\&quot;:[{\&quot;Number\&quot;:\&quot;WCU9799P\&quot;},{\&quot;Number\&quot;:\&quot;B1V48603\&quot;}],\&quot;RenewalEffectiveDate\&quot;:\&quot;09/28/2016\&quot;,\&quot;State\&quot;:\&quot;\&quot;}&quot;,
  &quot;resource&quot;: &quot;/{proxy+}&quot;,
  &quot;path&quot;: &quot;api/GetClaimsCount&quot;,
  &quot;httpMethod&quot;: &quot;POST&quot;,
  &quot;headers&quot;: {
    &quot;Accept&quot;: &quot;application/json&quot;,
    &quot;Accept-Encoding&quot;: &quot;gzip, deflate, sdch&quot;,
    &quot;Accept-Language&quot;: &quot;en-US,en;q=0.8&quot;
    }
}

Before the debugger hits the first line of my function handler, I get a long error that begins with:

System.Exception: Error deserializing the input JSON to type String
at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in ...

And here is the beginning of my function handler. I use this same code on the other lambdas and it works fine. I think this has something to do with the array part of this json. If I am right, then how do I format my request to accept json arrays?

 public Task&lt;APIGatewayHttpApiV2ProxyResponse&gt; FunctionHandler(string input, ILambdaContext context)
    {
        string? message = &quot;&quot;;
        string inputString = (input != null) ? JsonSerializer.Serialize(input) : 
string.Empty;
        _log.Debug(&quot;Request:&quot; + inputString);

答案1

得分: 0

出现了缺少闭括号的情况

{
  "body": "{\"PolicyNumbers\":[{\"Number\":\"WCU9799P\"},{\"Number\":\"B1V48603\"}],\"RenewalEffectiveDate\":\"09/28/2016\",\"State\":\"\"}",
  "resource": "/{proxy+}",
  "path": "api/GetClaimsCount",
  "httpMethod": "POST",
  "headers": {
    "Accept": "application/json",
    "Accept-Encoding": "gzip, deflate, sdch",
    "Accept-Language": "en-US,en;q=0.8"
  }
}

这个下一个括号是关闭"headers"还是整个对象"?

尝试在结尾处添加一个最终的},看看是否有帮助。

此外,像 https://jsonformatter.curiousconcept.com/ 这样的工具可能在这种情况下很有用,以查看JSON是否有效。

英文:

It appears there is a closing brace missing

{
  &quot;body&quot;: &quot;{\&quot;PolicyNumbers\&quot;:[{\&quot;Number\&quot;:\&quot;WCU9799P\&quot;},{\&quot;Number\&quot;:\&quot;B1V48603\&quot;}],\&quot;RenewalEffectiveDate\&quot;:\&quot;09/28/2016\&quot;,\&quot;State\&quot;:\&quot;\&quot;}&quot;,
  &quot;resource&quot;: &quot;/{proxy+}&quot;,
  &quot;path&quot;: &quot;api/GetClaimsCount&quot;,
  &quot;httpMethod&quot;: &quot;POST&quot;,
  &quot;headers&quot;: {
    &quot;Accept&quot;: &quot;application/json&quot;,
    &quot;Accept-Encoding&quot;: &quot;gzip, deflate, sdch&quot;,
    &quot;Accept-Language&quot;: &quot;en-US,en;q=0.8&quot;

.... does this next brace close &quot;headers&quot; or the entire object"?

}

Try adding a final } at the end and see if that helps.

Also tools like https://jsonformatter.curiousconcept.com/ could be quite handy in this situation to see if the JSON is valid.

答案2

得分: 0

我找到了问题。我需要将函数处理程序中的第一个参数更改为 "ApiGatewayRequest.Root input",因为请求是通过API Gateway传递的:

public Task<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(ApiGatewayRequest.Root input, ILambdaContext context)
{
    string? message = "";
    string inputString = (input != null) ? JsonSerializer.Serialize(input) : string.Empty;
    _log.Debug("Request:" + inputString);
}
英文:

I found the issue. I need to change the first parameter in the Function Handler to "ApiGatewayRequest.Root input" since the request is coming through the API Gateway:

 public Task&lt;APIGatewayHttpApiV2ProxyResponse&gt; FunctionHandler(ApiGatewayRequest.Root input, ILambdaContext context)
    {
        string? message = &quot;&quot;;
        string inputString = (input != null) ? JsonSerializer.Serialize(input) : 
    string.Empty;
    _log.Debug(&quot;Request:&quot; + inputString);

huangapple
  • 本文由 发表于 2023年2月10日 03:38:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定