来自浏览器的 C# AWS Lambda API 网关 - 参数不起作用
C# AWS Lambda API Gateway from browser - parameters not working
使用简单的 C# AWS Lambda
public string FunctionHandler(string myParam1, ILambdaContext context)
{
return myParam1;
}
我应该如何通过浏览器 GET 请求将参数传递给 AWS Lambda 函数 + API 网关?
例如,我想要这样的东西:
https://[API ID].execute-api.[REGION].amazonaws.com/myFunc?myParam1=myValue1
在浏览器中显示 {"message":"Internal Server Error"}
在日志中显示 Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example "Hello World" in order to be converted to a string: The JSON value could not be converted to System.String.
没有参数它可以工作,例如:
public string FunctionHandler(ILambdaContext context)
{
return Utf8Json.JsonSerializer.ToJsonString(context);
}
在浏览器中发送 GET 请求时 https://[API ID].execute-api.[REGION].amazonaws.com/myFunc
returns 成功 {"AwsRequestId":"86ca2da9-438c-4865-8a0b-29d3ced37176","FunctionName":...
.
好的,我找到了一个解决方案,而不是使用内置的参数解析,而是可以通过读取流来读取完整的 JSON 参数:
public string FunctionHandler(Stream requestStream, ILambdaContext context) { ... }
这里的requestStream里面会有GET/POST的参数或者更大的JSON的参数,但是要手动解析。请注意,参数可能会以 b64 编码(或可能也被压缩)发送。一个好方法是找到一个进行这种解析的库。
就我而言,我还编写了消费者 JS 代码,因此我可以确保参数始终以相同的方式出现,这样我的问题就解决了,但是如果有人知道一个好的库,请告诉我。
手动 POST 请求数据提取示例,它也支持 b64 编码:
public class StreamBody
{
public string body;
public bool isBase64Encoded;
}
public string FunctionHandler(Stream requestStream, ILambdaContext context)
{
using var sr = new StreamReader(requestStream);
var input = sr.ReadToEnd();
var sbody = Utf8Json.JsonSerializer.Deserialize<StreamBody>(input);
var body = !sbody.isBase64Encoded ? sbody.body : Encoding.UTF8.GetString(Convert.FromBase64String(sbody.body));
使用简单的 C# AWS Lambda
public string FunctionHandler(string myParam1, ILambdaContext context)
{
return myParam1;
}
我应该如何通过浏览器 GET 请求将参数传递给 AWS Lambda 函数 + API 网关?
例如,我想要这样的东西:
https://[API ID].execute-api.[REGION].amazonaws.com/myFunc?myParam1=myValue1
在浏览器中显示 {"message":"Internal Server Error"}
在日志中显示 Error converting the Lambda event JSON payload to a string. JSON strings must be quoted, for example "Hello World" in order to be converted to a string: The JSON value could not be converted to System.String.
没有参数它可以工作,例如:
public string FunctionHandler(ILambdaContext context)
{
return Utf8Json.JsonSerializer.ToJsonString(context);
}
在浏览器中发送 GET 请求时 https://[API ID].execute-api.[REGION].amazonaws.com/myFunc
returns 成功 {"AwsRequestId":"86ca2da9-438c-4865-8a0b-29d3ced37176","FunctionName":...
.
好的,我找到了一个解决方案,而不是使用内置的参数解析,而是可以通过读取流来读取完整的 JSON 参数:
public string FunctionHandler(Stream requestStream, ILambdaContext context) { ... }
这里的requestStream里面会有GET/POST的参数或者更大的JSON的参数,但是要手动解析。请注意,参数可能会以 b64 编码(或可能也被压缩)发送。一个好方法是找到一个进行这种解析的库。 就我而言,我还编写了消费者 JS 代码,因此我可以确保参数始终以相同的方式出现,这样我的问题就解决了,但是如果有人知道一个好的库,请告诉我。
手动 POST 请求数据提取示例,它也支持 b64 编码:
public class StreamBody
{
public string body;
public bool isBase64Encoded;
}
public string FunctionHandler(Stream requestStream, ILambdaContext context)
{
using var sr = new StreamReader(requestStream);
var input = sr.ReadToEnd();
var sbody = Utf8Json.JsonSerializer.Deserialize<StreamBody>(input);
var body = !sbody.isBase64Encoded ? sbody.body : Encoding.UTF8.GetString(Convert.FromBase64String(sbody.body));