英文:
request.Body has a different value through http gateway than through the test GUI
问题
我从https://github.com/aws/aws-lambda-go/blob/master/events/README_ApiGatewayEvent.md获取了这段代码:
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
fmt.Printf("Body size = %d.\n", len(request.Body))
fmt.Println("Headers:")
for key, value := range request.Headers {
fmt.Printf(" %s: %s\n", key, value)
}
log.Printf("%v", request.Body)
return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}
func main() {
lambda.Start(handleRequest)
}
我只添加了一行日志:log.Printf("%v", request.Body)
当我编译并使用lambda测试GUI时,我发送了以下内容:
{"body":"[{\"id\":\"123\"}]"}
在选项卡中的日志显示如下:
2021/08/16 08:51:50 [{"id":"123"}]
然而,当我执行以下操作时:
curl https://qmk743l1p0.execute-api.us-east-1.amazonaws.com/default/simplegateway -d '{"body":"[{\"id\":\"123\"}]"}' --header "Content-Type: application/json"
我在CloudWatch日志中得到了以下内容:
2021/08/16 08:56:07 {"body":"[{\"id\":\"123\"}]"}
我不知道为什么通过HTTP网关时request.body
会有两个不同的值。
如何在lambda测试GUI和HTTP API网关中获取预期的body
值?
英文:
I got this code from https://github.com/aws/aws-lambda-go/blob/master/events/README_ApiGatewayEvent.md :
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fmt.Printf("Processing request data for request %s.\n", request.RequestContext.RequestID)
fmt.Printf("Body size = %d.\n", len(request.Body))
fmt.Println("Headers:")
for key, value := range request.Headers {
fmt.Printf(" %s: %s\n", key, value)
}
log.Printf("%v", request.Body)
return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}
func main() {
lambda.Start(handleRequest)
}
I only added: the log: log.Printf("%v", request.Body)
When I compile and use the lambda test GUI, I send this:
{"body":"[{\"id\":\"123\"}]"}
and the log in the tab shows this:
2021/08/16 08:51:50 [{"id":"123"}]
However, when I do this:
curl https://qmk743l1p0.execute-api.us-east-1.amazonaws.com/default/simplegateway -d '{"body":"[{\"id\":\"123\"}]"}' --header "Content-Type: application/json"
I get this in the CloudWatch logs:
2021/08/16 08:56:07 {"body":"[{\"id\":\"123\"}]"}
I have no idea why request.body would have two different values when going through the http gateway.
How do I get the intended "body" in both the lambda test GUI and in through the http API gateway?
答案1
得分: 3
这是因为你的处理程序在两种情况下具有相同的签名,但根据代理请求的方式,其含义不同。
当你从Lambda GUI发送请求时,请求体会直接解组为events.APIGatewayProxyRequest
。该结构体定义了一个字段:
Body string `json:"body"`
因此,当你发送载荷{"body":"[{\"id\":\"123\"}]}"
时,json.Unmarshal
会将该字段填充为JSON的body
属性的值,即[{"id":"123"}]
。
当相同的请求通过API Gateway进行代理时,Gateway会构造一个包含原始载荷的AWS事件,因此它可能看起来像这样:
{
...
"body": "{\"body\":\"[{\"id\":\"123\"}]\"}"
}
然后,事件才会解组为events.APIGatewayProxyRequest
结构体,Body
字段的值为{"body":"[{\"id\":\"123\"}]}"
。
英文:
This happens because your handler has the same signature in both cases, but what that implies differs based on what is proxying your request.
When you send the request from the Lambda GUI, the body gets unmarshaled into the events.APIGatewayProxyRequest
straight away. That struct has a field defined as:
Body string `json:"body"`
So when you send the payload {"body":"[{\"id\":\"123\"}]"}
, json.Unmarshal
populates that field with the value of the JSON body
property, which is [{"id":"123"}]
When the same request is proxied through the API Gateway, the Gateway constructs a AWS event that contains your payload as-is, so it will probably look like:
{
...
"body": "{\"body\":\"[{\"id\":\"123\"}]\"}\"
}
And only then the event is unmarshaled into the events.APIGatewayProxyRequest
struct, resulting in the Body
field having the value {"body":"[{\"id\":\"123\"}]"}
.
答案2
得分: 2
尽管我对Lambda GUI不太熟悉,但我怀疑在那里你需要输入请求的JSON表示(包括头部和正文),但是当使用curl发送时,你不需要包装正文(-d),所以只需像这样发送它[{"id":"123"}]
,因为这是正文应该包含的内容。
英文:
Though i am not familiar with the lambda GUI i suspect that there you input the json representation of the request (with headers and body), but when sending via curl, you don't need to wrap the body (-d), so just send it like this [{\"id\":\"123\"}]
as this is what the body should contain.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论