在测试主体中传递Json时.Net Core 3.1 AWS Lambda错误 [英] .Net Core 3.1 AWS Lambda error when passing Json in test body
问题描述
我正在尝试测试我的AWS Lambda .Net Core 3.1 API.
I am trying to test my AWS Lambda .Net Core 3.1 API.
我使用适用于Visual Studio的AWS工具创建了一个标准应用程序,并添加了我的AesController.标准的在正文参数中不带参数的Get请求可以正常工作,但是当我尝试调用带参数的Get请求时,我会收到" 400错误的请求". "的异常.JSON值无法转换为System.String.路径:$ |行号:0 | BytePositionInLine:1."
I created a standard application using AWS tooling for Visual Studio and add my AesController. Standard Get request without parameters in body argument working fine, however when I try to call Get request with parameters I am getting "400 Bad Request" exception with "The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
测试功能:
[Fact]
public async Task TestGetEncrypt()
{
var lambdaFunction = new LambdaEntryPoint();
var requestStr = File.ReadAllText("./SampleRequests/AesController-GetEncrypt.json");
var request = JsonConvert.DeserializeObject<APIGatewayProxyRequest>(requestStr);
var context = new TestLambdaContext();
var response = await lambdaFunction.FunctionHandlerAsync(request, context);
Assert.Equal(200, response.StatusCode);
Assert.Equal("뺕結确", response.Body);
Assert.True(response.MultiValueHeaders.ContainsKey("Content-Type"));
Assert.Equal("application/json; charset=utf-8",
response.MultiValueHeaders["Content-Type"][0]);
}
设置Json:
{
"resource": "/{proxy+}",
"path": "/aes/GetEncrypt",
"httpMethod": "GET",
"headers": {
"Content-Type": "application/json"
},
"queryStringParameters": null,
"pathParameters": {
"proxy": "aes/GetEncrypt"
},
"stageVariables": null,
"requestContext": {
"accountId": "AAAAAAAAAAAA",
"resourceId": "5agfss",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": "AAAAAAAAAAAA",
"cognitoIdentityId": null,
"caller": "BBBBBBBBBBBB",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::AAAAAAAAAAAA:root",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_102)",
"user": "AAAAAAAAAAAA"
},
"resourcePath": "/{proxy+}",
"httpMethod": "GET",
"apiId": "t2yh6sjnmk"
},
"body": "{\"Data\": \"test_me!\"}"
}
API获取功能
// GET aes/Encrypt
[HttpGet("GetEncrypt")]
public async Task<IActionResult> GetEncrypt([FromBody] string content)
{
var jsonConvert = JsonConvert.DeserializeObject<JsonAes>(content);
var aesCrypt = new AesCrypt(jsonConvert);
return Ok(Encoding.Unicode.GetString(await aesCrypt.CryptAes()));
}
我认为自己的身体"状况不对劲.设置JSON中的参数.我试图通过普通的JSON格式,但不带有"字样.但是我遇到了同样的错误.
I believe that I am doing something wrong with my "body" argument in settings JSON. I tried to pass a normal JSON format without "" but I get the same error.
推荐答案
我设法找到一个解决方案. 我需要编辑get request函数参数以指向对象而不是纯字符串.
I manage to find a solution. I need to edit the get request function parameters to point to object rather plain string.
正确的代码:
// GET aes/Encrypt
[HttpGet("GetEncrypt")]
public async Task<IActionResult> GetEncrypt([FromBody] JsonAes content)
{
var aesCrypt = new AesCrypt(content);
return Ok(Encoding.Unicode.GetString(await aesCrypt.CryptAes()));
}
这篇关于在测试主体中传递Json时.Net Core 3.1 AWS Lambda错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!