为了账号安全,请及时绑定邮箱和手机立即绑定

Golang AWS API Gateway 寻找值开头的无效字符“e”

Golang AWS API Gateway 寻找值开头的无效字符“e”

Go
慕尼黑8549860 2023-04-24 15:53:30
我正在尝试创建一个连接到 lambda 的 API 网关,它使用 handlebars 解析 HTML 模板,然后返回它,但是当我在本地运行它时,甚至在使用 AWS 的测试 url 上运行时,我都收到了这个错误。{"errorMessage": "invalid character 'e' looking for beginning of value","errorType": "SyntaxError"}这是我的 SAM 模板AWSTemplateFormatVersion: "2010-09-09"Transform: AWS::Serverless-2016-10-31Description: data-template-rendererParameters:  Stage:    Type: String    AllowedValues:    - dev    - staging    - production    Description: environment valuesResources:  # Defining the API Resource here means we can define the stage name rather than  # always being forced to have staging/prod. Also means we can add a custom domain with  # to the API gateway within this template if needed. Unfortunately there is a side effect  # where it creates a stage named "Stage". This is a known bug and the issue can be  # found at https://github.com/awslabs/serverless-application-model/issues/191  DataTemplateRendererApi:    Type: AWS::Serverless::Api    Properties:      Name: !Sub "${Stage}-data-template-renderer"      StageName: !Ref Stage      DefinitionBody:        swagger: "2.0"        basePath: !Sub "/${Stage}"        info:          title: !Sub "${Stage}-data-template-renderer"          version: "1.0"        consumes:        - application/json        produces:        - application/json        - text/plain        - application/pdf        paths:          /render:            post:              x-amazon-apigateway-integration:                uri:                  "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RenderTemplate.Arn}/invocations"                httpMethod: POST                type: aws_proxy        x-amazon-apigateway-binary-media-types:        - "*/*"下面是我用于 Lambda 的代码。请原谅它可能不是您见过的最好的 Golang 代码,但我是 Golang 的初学者,因为我主要是一名 PHP 开发人员,但我工作的公司正在创建很多 Golang lambda,所以我开始学习它。
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

我决定查看request.Body,似乎通过将其添加*/*到 API 网关二进制媒体类型,现在即使请求也是编码的,你看第一个字母确实是eas消息说。


所以我改变了这个:


// Unmarshal json request body into a TemplateRendererRequest struct that mimics the json payload

    requestData := TemplateRendererRequest{}

    err := json.Unmarshal([]byte(request.Body), &requestData)

    if err != nil {

        return events.APIGatewayProxyResponse{

            Body: fmt.Errorf("Error: %s ", err.Error()).Error(),

            StatusCode: 400,

            Headers: map[string]string{

                "Content-Type": "text/plain",

            },

        }, err

    }

对此:


// Unmarshal json request body into a TemplateRendererRequest struct that mimics the json payload

    requestData := TemplateRendererRequest{}


    b64String, _ := base64.StdEncoding.DecodeString(request.Body)

    rawIn := json.RawMessage(b64String)

    bodyBytes, err := rawIn.MarshalJSON()

    if err != nil {

        return events.APIGatewayProxyResponse{

            Body: fmt.Errorf("Error: %s ", err.Error()).Error(),

            StatusCode: 400,

            Headers: map[string]string{

                "Content-Type": "text/plain",

            },

        }, err

    }


    jsonMarshalErr := json.Unmarshal(bodyBytes, &requestData)

    if jsonMarshalErr != nil {

        return events.APIGatewayProxyResponse{

            Body: fmt.Errorf("Error: %s ", jsonMarshalErr.Error()).Error(),

            StatusCode: 400,

            Headers: map[string]string{

                "Content-Type": "text/plain",

            },

        }, jsonMarshalErr

    }

从我在网上看到的你也可以改变这个:


rawIn := json.RawMessage(b64String)

bodyBytes, err := rawIn.MarshalJSON()

对此:


[]byte(b64String)

当我现在执行 CURL 请求并将其输出到文件时,我会正确地获得 PDF。


查看完整回答
反对 回复 2023-04-24
  • 1 回答
  • 0 关注
  • 62 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信