Amazon API Gateway HTTP API:go 中 lambda 函数中的自定义类型

对于如何使用 golang 并坐在HttpApi后面将自定义类型传递到我的 Lambda 函数中,我有点困惑。


考虑以下 go lambda 处理程序,它几乎是文档中示例的副本。



type MyRequestType struct {

    Name string `json:"name"`

    Age  int    `json:"age"`

}


type MyResponseType struct {

    Message string `json:"message"`

}


func handler(request MyRequestType) (MyResponseType, error) {

    log.Printf("received request: %v", request)

    return MyResponseType{Message: fmt.Sprintf("Hello %s, you are %d years old!", request.Name, request.Age)}, nil

}


func main() {

    lambda.Start(handler)

}

结果消息始终如下。


{

    "message": "Hello , you are 0 years old!"

}

我有一种转储的感觉,这是不可能的Amazon API Gateway HTTP API。但我也没有找到任何文件指出这是不可能的。所以我真的想知道,如果我做错了什么?


该文档还说明了有关有效签名的一些信息:


例如func (context.Context, TIn) (TOut, error)


如果我使用的HTTP API是Payload format version 2:


是context.Context普通的golangcontext还是特别的?我在想events.APIGatewayV2HTTPRequestContext或其他人。


TInand TOut=> events.APIGatewayV2HTTPRequestand的正确类型是events.APIGatewayV2HTTPResponse什么?


慕莱坞森
浏览 139回答 1
1回答

青春有我

context.Context 是不是普通的golang上下文是的。但是您可以获得lambdacontext.FromContext包含额外 lambda 特定元数据的 Lambda 上下文。什么是正确的 TIn 和 TOut 类型这取决于谁调用了 Lambda。当 Lambda 被另一个 AWS 服务调用时,包括 API 网关,所谓的TIn和TOut是来自lambdaevent包的类型。引用包介绍:此程序包为处理 AWS 事件的 Lambda 函数提供输入类型。在 API 网关的情况下,这将是events.APIGatewayProxyRequestand Response,或者可能是在 v1.16.0 中添加的Payload format version 2and events.APIGatewayV2HTTPRequest。Responsegithub repo README中的更多文档(但不多)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go