英文:
.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:
{
"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"
}
}
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<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(string input, ILambdaContext context)
{
string? message = "";
string inputString = (input != null) ? JsonSerializer.Serialize(input) :
string.Empty;
_log.Debug("Request:" + 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
{
"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"
.... does this next brace close "headers"
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<APIGatewayHttpApiV2ProxyResponse> FunctionHandler(ApiGatewayRequest.Root input, ILambdaContext context)
{
string? message = "";
string inputString = (input != null) ? JsonSerializer.Serialize(input) :
string.Empty;
_log.Debug("Request:" + inputString);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论