在测试主体中传递 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.在 body 参数中没有参数的标准 Get 请求工作正常,但是当我尝试使用参数调用 Get 请求时,我收到400 Bad Request";"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 请求函数参数以指向对象而不是普通字符串.
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屋!