一尘不染

通过API GW调用时AWS Lambda Go函数未获取请求主体

go

首先,可能会说这个问题与HTTP请求正文非常相似,即它没有通过AWS API Gateway到达AWS
Lambda函数,或者没有通过API网关在AWSLambda中获取json正文

但是,这些问题都没有解决使用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)
}

问题:MyReturn从API GW调用时,对象未填充任何值。该行log.Print("Called by ", name)导致没有任何内容附加到Called by字符串。

向API GW的请求:

 POST -> body: '{"name":"Bob"}', headers: {'Content-Type': 'application/json'}

这是在纯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));
}

但是,从AWS Lambda控制台进行测试时,完全相同的主体可以工作。

身体:

{"name":"Bob"}

阅读 279

收藏
2020-07-02

共1个答案

一尘不染

事实证明,即使我无法在面向用户的网站上找到与此相关的任何文档,也确实存在文档。阅读此内容: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

2020-07-02