第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

無法從 Amazon API Gateway for Golang 將參數(shù)從 POST

無法從 Amazon API Gateway for Golang 將參數(shù)從 POST

Go
牛魔王的故事 2022-04-20 19:31:06
它不同于 -如何將參數(shù)從 POST 傳遞到來自 Amazon API Gateway 的 AWS Lambda。因為我能夠轉(zhuǎn)換 API Gateway 中的參數(shù),但不能傳遞它或用 Golang 打印它。但是,相同的 API 網(wǎng)關(guān)適用于 Python。下面是我的 API Gateway 和 AWS Lambda(Golang) 的日志。我可以看到 POST 參數(shù)已成功轉(zhuǎn)換為 JSON。不過,我在 Lambda 函數(shù)日志中看不到它。API 網(wǎng)關(guān)日志(442f74ed-39e5-4372-bf85-42bf814f802f) Extended Request Id: EIaYxxMF3lQ=(442f74ed-39e5-4372-bf85-42bf814f802f) Method request path: {}(442f74ed-39e5-4372-bf85-42bf814f802f) Method request query string:    {}(442f74ed-39e5-4372-bf85-42bf814f802f) Method request headers: {Accept=*/*, Cache-Control=max-age=259200, X-Twilio-Signature=ZWg2v7xxxfnBlPyxE=, User-Agent=TwilioProxy/1.1, X-Forwarded-Proto=https, I-Twilio-Idempotency-Token=e5d1xxx221bc4, X-Forwarded-For=54.xxxx.227, Host=xxxxxxx.execute-api.us-east-1.amazonaws.com, X-Forwarded-Port=443, X-Amzn-Trace-Id=Root=1-5de67103-7994dbxxx0dbd872, Content-Type=application/x-www-form-urlencoded}}  (442f74ed-39e5-4372-bf85-42bf814f802f) Endpoint request URI: https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:6xxxxxxxx6:function:Twillio_connector_test/invocations(442f74ed-39e5-4372-bf85-42bf814f802f) Endpoint request body after transformations:     {        "ToCountry": "US",        "ToState": "UT",        "SmsMessageSid": "SMed65aaxxxxxx5c7938df",        "NumMedia": "0",        "ToCity": "",        "FromZip": "",        "SmsSid": "SMed65aaxxxxxx938df",        "FromState": "",        "SmsStatus": "received",        "FromCity": "",        "Body": "Testing+again",        "FromCountry": "IN",        "To": "%2B1xxxxxx848",        "ToZip": "",        "NumSegments": "1",        "MessageSid": "SMed65aa5dxxxx7938df",        "AccountSid": "AC23xxxd98",        "From": "%2B9xxxxxx90",        "ApiVersion": "2010-04-01"    }我嘗試了很多方法。但是我無法在 Golang 代碼中獲取所需的參數(shù)。如果我做錯了什么,請糾正我。任何幫助將不勝感激。
查看完整描述

1 回答

?
ITMISS

TA貢獻1871條經(jīng)驗 獲得超8個贊

結(jié)論 :

這個問題是由 golang 和 python 中 lambda handler 的 param struct 不同引起的。


細節(jié):

API Gateway 映射模板制作的 JSON:

    {

        "ToCountry": "US",

        "ToState": "UT",

        "SmsMessageSid": "SMed65aaxxxxxx5c7938df",

        "NumMedia": "0",

        "ToCity": "",

        "FromZip": "",

        "SmsSid": "SMed65aaxxxxxx938df",

        "FromState": "",

        "SmsStatus": "received",

        "FromCity": "",

        "Body": "Testing+again",

        "FromCountry": "IN",

        "To": "%2B1xxxxxx848",

        "ToZip": "",

        "NumSegments": "1",

        "MessageSid": "SMed65aa5dxxxx7938df",

        "AccountSid": "AC23xxxd98",

        "From": "%2B9xxxxxx90",

        "ApiVersion": "2010-04-01"

    }

eventpython中的參數(shù):

    def lambda_handler(event, context):

        print("Received event: " + str(event))

        return '<?xml version=\"1.0\" encoding=\"UTF-8\"?>'\

           '<Response><Message>Hello world! -Lambda</Message></Response>'

參考AWS Python 文檔:


事件 – AWS Lambda 使用此參數(shù)將事件數(shù)據(jù)傳遞給處理程序。該參數(shù)通常是 Python dict 類型。它也可以是列表、str、int、float 或 NoneType 類型。


【重要部分】events.APIGatewayProxyRequest golang中的參數(shù):

// APIGatewayProxyRequest contains data coming from the API Gateway proxy

type APIGatewayProxyRequest struct {

    Resource                        string                        `json:"resource"` // The resource path defined in API Gateway

    Path                            string                        `json:"path"`     // The url path for the caller

    HTTPMethod                      string                        `json:"httpMethod"`

    Headers                         map[string]string             `json:"headers"`

    MultiValueHeaders               map[string][]string           `json:"multiValueHeaders"`

    QueryStringParameters           map[string]string             `json:"queryStringParameters"`

    MultiValueQueryStringParameters map[string][]string           `json:"multiValueQueryStringParameters"`

    PathParameters                  map[string]string             `json:"pathParameters"`

    StageVariables                  map[string]string             `json:"stageVariables"`

    RequestContext                  APIGatewayProxyRequestContext `json:"requestContext"`

    Body                            string                        `json:"body"`

    IsBase64Encoded                 bool                          `json:"isBase64Encoded,omitempty"`

}

所以你可以看到,你提供給 Golang AWS lambda handler 的 json map 只匹配一個 key Body。這就是 printf 只打印Testing Again.


解決方案:

要使用 golang 編寫的 lambda 獲得正確的結(jié)果,我認為您可以參考AWS DOC 的這一部分。(這對我們了解 AWS API Gateway 映射模板的工作原理很有幫助。)


重寫映射模板以匹配APIGatewayProxyRequestJSON 標記。


查看完整回答
反對 回復(fù) 2022-04-20
  • 1 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號