Skip to content
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

Closed
jeffotoni opened this issue Jun 18, 2022 · 4 comments
Closed

Lambda function URLs How to Read Body from Post? #4447

jeffotoni opened this issue Jun 18, 2022 · 4 comments
Assignees
Labels
documentation This is a problem with documentation. p3 This is a minor priority issue

Comments

@jeffotoni
Copy link

jeffotoni commented Jun 18, 2022

Describe the issue

Ex:
curl -i -XPOST -H "Content-Type: application/json" https://xxxxxxx.lambda-url.us-east-1.on.aws -d '{"input":"ping"}'

type Event struct {
	Input string `json:"input"`
}

func HandleRequest(event Event) (*Response, error) {
	if len(event.Input) > 0 && event.Input == "ping" {
		return &Response{Msg: "pong"}, nil
	}
	return &Response{Msg: "I don't know"}, nil
}

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

@jeffotoni jeffotoni added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Jun 18, 2022
@Jonniedev
Copy link

@RanVaknin RanVaknin added the p3 This is a minor priority issue label Mar 31, 2023
@RanVaknin RanVaknin self-assigned this May 10, 2023
@RanVaknin
Copy link
Contributor

RanVaknin commented Jul 19, 2023

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,
Thanks,
Ran~

@RanVaknin RanVaknin added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Jul 19, 2023
@jeffotoni
Copy link
Author

I'm going to test it here but it turned out pretty simple lol 😁

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jul 20, 2023
Copy link

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. p3 This is a minor priority issue
Projects
None yet
Development

No branches or pull requests

4 participants