我试图在lambda函数中解析来自events.ALBTargetGroupRequest的请求体。
对于如何解析这些二进制数据,我找不到任何示例或解释。我的想法是,我会得到一个proto,只是解开它,但事实并非如此。流程是gRPC-->ALB-->Lambda.
Lambda代码示例:
func HandleRequest(ctx context.Context, request events.ALBTargetGroupRequest) (events.ALBTargetGroupResponse, error) {
// Assume body is base64 encoded
bt, err := base64.StdEncoding.DecodeString(request.Body)
if err != nil {
log.Printf("Failed to decode body: %s\n", err.Error())
return events.ALBTargetGroupResponse{}, err
}
msg := pb.Msg{}
err = proto.Unmarshal(bt, &msg)
if err != nil {
log.Printf("Failed to unmarshal body: %s\n", err.Error())
return events.ALBTargetGroupResponse{}, err
}
return events.ALBTargetGroupResponse{Body: "", StatusCode: 200, StatusDescription: "200 OK", IsBase64Encoded: false, Headers: map[string]string{}}, nil
}
func main() {
lambda.Start(HandleRequest)
}标题:
{
"accept-encoding": "identity,gzip",
"content-type": "application/grpc",
"grpc-accept-encoding": "identity,deflate,gzip",
"grpc-timeout": "353S",
"te": "trailers",
"user-agent": "grpc-c++/1.16.0 grpc-c/6.0.0 (linux; chttp2; gao)",
"x-amzn-trace-id": "Root=1-6124edf6-4fa33c5674127c7266ba0430"
}错误:
Failed to Unmarshal body: proto: cannot parse invalid wire-format data
proto: cannot parse invalid wire-format data: prefixError字节数组:(如果有帮助的话)
[0 0 0 0 85 10 83 10 43 105 112 45 49 48 45 49 54 53 45 50 50 53 45 55 53 46 101 117 45 119 101 115 116 45 50 46 99 111 109 112 117 116 101 46 105 110 116 101 114 110 97 108 18 36 102 56 56 56 101 100 53 99 45 54 102 99 51 45 52 99 54 99 45 56 49 99 55 45 55 53 101 100 48 99 57 50 54 102 50 53]如果有人能对这个问题有所了解,我将不胜感激。
发布于 2021-08-25 20:24:29
看来,events.ALBTargetGroupRequest是一个普通的Golang (已经为您解封)。
请参阅:https://github.com/aws/aws-lambda-go/blob/master/events/README_ALBTargetGroupEvents.md
您不需要(Un)封送request,只需使用它,就像处理它的响应同胞event.ALBTargetGroupResponsebody一样。
https://stackoverflow.com/questions/68908912
复制相似问题