在Go中,没有使用API Gateway的情况下,AWS Lambda函数的URL是什么?

huangapple go评论81阅读模式
英文:

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.APIGatewayProxyRequestGET方法。但是当我尝试将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

huangapple
  • 本文由 发表于 2022年6月29日 12:43:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72795881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定