rulisastra
rulisastra

Reputation: 391

AWS lambda's function URL without API Gateway in Go

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.

Upvotes: 5

Views: 2100

Answers (1)

blackgreen
blackgreen

Reputation: 45011

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

Upvotes: 12

Related Questions