-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lambda function URLs How to Read Body from Post? #4447
Comments
Hi @jeffotoni when making a curl request directly you need to unmarshal the body from event before you can access your input. package main
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
type Body struct {
Input string `json:"input"`
}
type Event struct {
Body string `json:"body"`
}
type Response struct {
StatusCode int `json:"statusCode"`
Body string `json:"body"`
}
func HandleRequest(ctx context.Context, event Event) (Response, error) {
var body Body
err := json.Unmarshal([]byte(event.Body), &body)
if err != nil {
return Response{StatusCode: 400, Body: `{"msg": "error ready body, Invalid JSON"}`}, err
}
if body.Input == "ping" {
return Response{StatusCode: 200, Body: `{"msg": "pong"}`}, nil
}
return Response{StatusCode: 200, Body: `{"msg": "I don't know"}`}, nil
}
func main() {
lambda.Start(HandleRequest)
} $ curl -i -XPOST -H "Content-Type: application/json" https://xxxxxxx.lambda-url.us-east-1.on.aws/ -d '{"input":"ping"}'
HTTP/1.1 200 OK
Date: Wed, 19 Jul 2023 00:06:57 GMT
Content-Type: application/json
Content-Length: 15
Connection: keep-alive
x-amzn-RequestId: REDACTED
X-Amzn-Trace-Id: REDACTED
{"msg": "pong"}% Hope this helps, |
I'm going to test it here but it turned out pretty simple lol 😁 |
Comments on closed issues are hard for our team to see. |
Describe the issue
Ex:
curl -i -XPOST -H "Content-Type: application/json" https://xxxxxxx.lambda-url.us-east-1.on.aws -d '{"input":"ping"}'
How to read the body coming from a Post ?
Because the example above didn't work for the curl Payload but for the event it works 100%.
Links
https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html
https://docs.aws.amazon.com/lambda/latest/dg/lambda-golang.html
The text was updated successfully, but these errors were encountered: