douqiang7462 2018-09-05 20:24
浏览 98
已采纳

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

First off, one might say this question is very similar to HTTP request body not getting to AWS lambda function via AWS API Gateway or Getting json body in aws Lambda via API gateway

However, none of these questions address using Golang, and the problem I have been having is finding an equivalent of the event parameter used everywhere in the Node.js documentation.

Here's my Lambda Function:

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)
}

Problem: the MyReturn object is not being filled with any value when called from API GW. The line log.Print("Called by ", name) results in nothing being appended to the Called by string.

Request to API GW:

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

This is being performed in pure JS as follows:

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));
}

And yet, the exact same body works when testing it from the AWS Lambda console.

Body:

{"name":"Bob"}
  • 写回答

1条回答 默认 最新

  • dqt20140129 2018-09-05 20:24
    关注

    Turns out, even though I wasn't able to find any documentation on this on a user-facing website, documentation does exist. Read this: https://github.com/aws/aws-lambda-go/blob/master/events/README_ApiGatewayEvent.md

    Here's the simplest way I've figured out so far to receive data from and respond to a request from 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)
    }
    

    In this case, the log.Print("Request body: ", name) line results in the exact request body being logged. Problem solved.

    Note: Also I didn't have to create that APIGWResponse object from the question, the events.APIGatewayProxyResponse is the exact same thing, already made for you. These objects are all inside this class: https://github.com/aws/aws-lambda-go/blob/master/events/apigw.go

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵