亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號