英文:
AWS lambda's function URL without API Gateway in Go
问题
我有一个通过API Gateway调用的Lambda函数。
该函数使用GET方法,并使用以下代码工作:
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// 在这里添加代码
return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}
func main() {
lambda.Start(handleRequest)
}
我有这个event.APIGatewayProxyRequest
和GET
方法。但是当我尝试将URL迁移到Function URLs时,我没有找到类似的设置GET
方法的选项。URL应该如何知道在POSTMAN中使用哪种方法?
是否有一个等效的event.APIGatewayProxyRequest
来执行请求?
当我使用URL调用它时,我得到了一个502
BAD GATEWAY错误。
英文:
I have a lambda function that is invoked with API Gateway.
The function is working using the GET method with this code:
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
// add code here
return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}
func main() {
lambda.Start(handleRequest)
}
I have this event.APIGatewayProxyRequest
and GET
method. But when I try to migrate the URL to a Function URLs, I have nothing similar to set the GET
method. How was the URL supposed to know which method to use in the POSTMAN? and...
Do we have an equivalent for event.APIGatewayProxyRequest
to do the request?
When I invoke it with URL, I got a 502
BAD GATEWAY error.
答案1
得分: 10
AWS Lambda函数URL的事件是events.LambdaFunctionURLRequest
,相关类型请参考这里。
因此,您的处理程序签名应该如下所示:
func handleRequest(ctx context.Context, request events.LambdaFunctionURLRequest) (events.LambdaFunctionURLResponse, error) {
// 在这里添加代码
return events.LambdaFunctionURLResponse{Body: request.Body, StatusCode: 200}, nil
}
一旦您创建了Lambda的URL,调用者(例如Postman)可以指定任何HTTP方法,您可以在处理程序内部访问它:
// request 是 LambdaFunctionURLRequest
request.RequestContext.HTTP.Method
英文:
The AWS event for Lambda Function URL is events.LambdaFunctionURLRequest
and the related types.
So your handler signature should look like:
func handleRequest(ctx context.Context, request events.LambdaFunctionURLRequest) (events.LambdaFunctionURLResponse, error) {
// add code here
return events.LambdaFunctionURLResponse{Body: request.Body, StatusCode: 200}, nil
}
Once you create the URL of your Lambda, the caller (e.g. Postman) can specify whatever HTTP method, which you can access inside your handler with:
// request is LambdaFunctionURLRequest
request.RequestContext.HTTP.Method
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论