Bim*_*mde 9 go aws-lambda aws-api-gateway aws-lambda-go
首先,有人可能会说这个问题与HTTP request body not getting to AWS lambda function via AWS API Gateway或Getting json body in aws Lambda via API gateway非常相似
但是,这些问题都没有解决使用 Golang 的问题,我遇到的问题是找到event与 Node.js 文档中随处使用的参数等效的参数。
这是我的 Lambda 函数:
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"`
}
type APIGWResponse struct {
IsBase64Encoded bool `json:"isBase64Encoded"`
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
func handle(ctx context.Context, name MyReturn) (APIGWResponse, error) {
log.Print("Called by ", 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 APIGWResponse{true, code, headers, string(response)}, nil
}
func main() {
lambda.Start(handle)
}
Run Code Online (Sandbox Code Playgroud)
问题:MyReturn从 API GW 调用时,对象未填充任何值。该行log.Print("Called by ", name)不会在Called by字符串中附加任何内容。
对 API GW 的请求:
POST -> body: '{"name":"Bob"}', headers: {'Content-Type': 'application/json'}
Run Code Online (Sandbox Code Playgroud)
这是在纯 JS 中执行的,如下所示:
const BASE_URL = "https://my_api_id.execute-api.us-east-1.amazonaws.com/prod/";
const TRIGGER_URL = "my_lambda_function";
function toGW() {
fetch(BASE_URL + TRIGGER_URL, {
method: 'POST',
body: '{"name":"Bimesh"}',
headers:{
'Content-Type': 'application/json'
}
})
.then(data => data.json())
.then(json => console.log(json))
.catch(error => console.log(error));
}
Run Code Online (Sandbox Code Playgroud)
然而,在从 AWS Lambda 控制台测试时,完全相同的主体可以工作。
身体:
{"name":"Bob"}
Run Code Online (Sandbox Code Playgroud)
Bim*_*mde 12
事实证明,即使我无法在面向用户的网站上找到任何关于此的文档,文档确实存在。阅读:https : //github.com/aws/aws-lambda-go/blob/master/events/README_ApiGatewayEvent.md
这是迄今为止我发现的从 API GW 接收数据并响应来自 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)
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该log.Print("Request body: ", name)行会记录准确的请求正文。问题解决了。
注意:我也不必APIGWResponse从问题中创建该对象,这events.APIGatewayProxyResponse是完全相同的东西,已经为您制作了。这些对象都在这个类中:https : //github.com/aws/aws-lambda-go/blob/master/events/apigw.go