首先,有人可能会说这个问题与HTTP request body not getting to AWS lambda function via AWS API Gateway或Getting json body in aws Lambda via API gateway
非常相似但是,这些问题都没有使用Golang解决,而我一直遇到的问题是找到与Node.js文档中各处使用的service
参数等效的问题。
这是我的Lambda函数:
pl/pgsql
问题:从API GW调用service
对象时未填充任何值。 event
行导致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)
}
字符串中没有任何内容。
对API GW的请求:
MyReturn
这是在纯JS中执行的,如下所示:
log.Print("Called by ", name)
但是,从AWS Lambda控制台进行测试时,完全相同的主体可以工作。
身体:
Called by
答案 0 :(得分:2)
结果证明,即使我无法在面向用户的网站上找到任何文档,也确实存在文档。阅读:https://github.com/aws/aws-lambda-go/blob/master/events/README_ApiGatewayEvent.md
到目前为止,这是我到目前为止最简单的从API GW接收数据并响应请求的方法:
POST -> body: '{"name":"Bob"}', headers: {'Content-Type': 'application/json'}
在这种情况下,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));
}
行导致记录了确切的请求正文。问题解决了。
注意:同样,我也不必从问题中创建那个{"name":"Bob"}
对象,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)
}
是完全一样的东西,已经为您量身定做了。这些对象都在此类中:https://github.com/aws/aws-lambda-go/blob/master/events/apigw.go
答案 1 :(得分:1)
好吧,我没有足够的声誉来回应@laurids的评论……所以我想StackOverflow迫使我做出一个新的答案(?)。大声笑
我很惊讶地发现,当我发送以下lambda测试事件时,我在golang中得到了一个空的请求正文
{
"name": "Bob"
}
我必须这样做才能设置请求正文。希望对您有帮助!
{
"body": "{\"name\": \"Bob\"}"
}