我正在尝试从我的 lambda 函数中的 events.ALBTargetGroupRequest
解析请求主体。
我找不到任何关于如何解析此二进制数据的示例或解释。我的想法是我会得到一个原型(prototype),然后将其解码,但事实并非如此。 流程是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]
如果有人能阐明这个主题,我将不胜感激。
最佳答案
看起来 events.ALBTargetGroupRequest
是一个常规的 Golang 结构(已经为您解码)。
参见:https://github.com/aws/aws-lambda-go/blob/master/events/README_ALBTargetGroupEvents.md
您不需要(取消)编码 request
并且可以像处理它的响应兄弟 event.ALBTargetGroupResponsebody
一样处理它。
关于go - 使用 gRPC 解析(解码)由 ALB 触发的 AWS lambda 请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68908912/