在AWS上测试Lambda函数时遇到问题
I have been trying to get my first lambda function in Go running via the Amazon API Gateway.
I have the following package set up in go. The goal is to send a JSON request and log and return the body of that request:
package main
import (
"net/http"
"log"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
)
func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
//These log statements return empty regardless of JSON input.
log.Print(request.body)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: request.Body
}, nil
}
func main() {
lambda.Start(Handler)
}
I can build and zip this and upload it to the AWS lambda manager. The AWS lambda manager contains an option to use a test event which I can configure with a JSON string
{
"testint": 1,
"teststring": "test"
}
However, if I run this test, I get the following result:
{
"statusCode": 200,
"headers": null,
"body": ""
}
I would expect the body to actually contain the json I passed to the function, but clearly something is going wrong.
我一直试图通过Amazon API Gateway运行Go中的第一个lambda函数。 p> \ n
我正在安装以下软件包。 目标是发送JSON请求并记录并返回该请求的正文: p>
包main
import(
“ net / http”
“ log “
” github.com/aws/aws-lambda-go/lambda"
“ github.com/aws/aws-lambda-go/events"
)
func Handler(request events.APIGatewayProxyRequest)( events.APIGatewayProxyResponse,error){
//无论JSON输入如何,这些日志语句都返回空。
log.Print(request.body)
返回事件。APIGatewayProxyResponse{
StatusCode:http.StatusOK,
正文:request.Body
},nil
}
func main(){
lambda.Start(Handler)
}
code> pre>
我可以构建并压缩它,然后将其上传到AWS lambda管理器。
AWS lambda管理器包含一个选项,该选项可以使用测试事件,可以使用JSON字符串对其进行配置 p>
{
“ testint”:1,
“ teststring”:“ test”
}
code> pre>
但是,如果我运行此测试,则会得到 以下结果: p>
{
“ statusCode”:200,
“ headers”:null,
“ body”:“”
}
code> pre>
我希望正文实际上包含我传递给函数的json,但显然出了点问题 p>
div>
There's a few minor things I changed and then it works
First, log.Print(request.body)
doesn't compile for me, but using request.Body
is fine
Second, the type you are using for the request is
// APIGatewayProxyRequest contains data coming from the API Gateway proxy
type APIGatewayProxyRequest struct {
Resource string `json:"resource"` // The resource path defined in API Gateway
Path string `json:"path"` // The url path for the caller
HTTPMethod string `json:"httpMethod"`
Headers map[string]string `json:"headers"`
QueryStringParameters map[string]string `json:"queryStringParameters"`
PathParameters map[string]string `json:"pathParameters"`
StageVariables map[string]string `json:"stageVariables"`
RequestContext APIGatewayProxyRequestContext `json:"requestContext"`
Body string `json:"body"`
IsBase64Encoded bool `json:"isBase64Encoded,omitempty"`
}
and in this Body is a field "body" that's a string. So altering your test data to
{
"body": "HELLO"
}
will give some data that passes through
Lastly, the parameters for the Handler in all the examples seems to include a context object, so I added that
func Handler(ctx context.Context, request events.APIGatewayProxyRequest)
Here is a complete version of your program that "worked for me"
package main
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"log"
"net/http"
)
func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
//These log statements return empty regardless of JSON input.
log.Print(request.Body)
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: request.Body}, nil
}
func main() {
lambda.Start(Handler)
}