1 回答
TA贡献1809条经验 获得超8个赞
事实证明,即使我无法在面向用户的网站上找到任何关于此的文档,文档也确实存在。阅读: https: //github.com/aws/aws-lambda-go/blob/master/events/README_ApiGatewayEvent.md
这是迄今为止我想出的从 API GW 接收数据和响应请求的最简单方法:
package main
import (
"context"
"encoding/json"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"
"log"
)
type myReturn struct {
Response string `json:"response"`
}
func handle(ctx context.Context, name events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
log.Print("Request body: ", name)
log.Print("context ", ctx)
headers := map[string]string{"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"}
code := 200
response, error := json.Marshal(myReturn{Response:"Hello, " + name.Body})
if error != nil {
log.Println(error)
response = []byte("Internal Server Error")
code = 500
}
return events.APIGatewayProxyResponse {code, headers, string(response), false}, nil
}
func main() {
lambda.Start(handle)
}
在这种情况下,该log.Print("Request body: ", name)行会记录准确的请求正文。问题解决了。
注意:我也不必APIGWResponse从问题中创建那个对象,这events.APIGatewayProxyResponse是完全一样的东西,已经为你做了。这些对象都在这个类里面: https: //github.com/aws/aws-lambda-go/blob/master/events/apigw.go
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报