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

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

使用 API Gateway 在 AWS Python Lambda 函數中嘗試訪問參數時出錯

使用 API Gateway 在 AWS Python Lambda 函數中嘗試訪問參數時出錯

DIEA 2021-12-16 15:58:12
我有這個 python lambda 函數import jsondef lambda_handler(event, context):    post_user = ""    post_user = event["user"]    print(post_user)            return {        "statusCode": 200,        "headers": {"Content-Type": "application/json"},        "body": True        }當我在 lambda IDE 中運行測試時,這按預期工作。測試配置為通過:{“用戶”:“約翰”,“密碼”:“密碼1”}但是當我使用 API Gateway 運行測試時,我收到此錯誤:Mon Mar 25 20:47:29 UTC 2019:轉換前的端點響應主體:{"errorMessage": "'user'", "errorType": "KeyError", "stackTrace": [" File \"/var/task/ lambda_function.py\", line 6, in lambda_handler\n post_user = event[\"user\"]\n"]} Mon Mar 25 20:47:29 UTC 2019:由于客戶函數錯誤,Lambda 執行失敗,狀態為 200 :'用戶'。Lambda 請求 ID:f7955f74-e608-4b10-b216-4e4acf682307 Mon Mar 25 20:47:29 UTC 2019:方法完成,狀態:502我已經定義了 API 網關測試如下:
查看完整描述

2 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

這是因為當event對象來自 API Gateway 時,它上面有一些額外的信息。它不像您用來從控制臺進行測試的 JSON 那樣簡單。


您需要首先訪問該body對象,然后最后訪問您的 JSON 對象。


以下是來自 API Gateway 的事件的樣子:


{

    "path": "/test/hello",

    "headers": {

      "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",

      "Accept-Encoding": "gzip, deflate, lzma, sdch, br",

      "Accept-Language": "en-US,en;q=0.8",

      "CloudFront-Forwarded-Proto": "https",

      "CloudFront-Is-Desktop-Viewer": "true",

      "CloudFront-Is-Mobile-Viewer": "false",

      "CloudFront-Is-SmartTV-Viewer": "false",

      "CloudFront-Is-Tablet-Viewer": "false",

      "CloudFront-Viewer-Country": "US",

      "Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",

      "Upgrade-Insecure-Requests": "1",

      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",

      "Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",

      "X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",

      "X-Forwarded-For": "192.168.100.1, 192.168.1.1",

      "X-Forwarded-Port": "443",

      "X-Forwarded-Proto": "https"

    },

    "pathParameters": {

      "proxy": "hello"

    },

    "requestContext": {

      "accountId": "123456789012",

      "resourceId": "us4z18",

      "stage": "test",

      "requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",

      "identity": {

        "cognitoIdentityPoolId": "",

        "accountId": "",

        "cognitoIdentityId": "",

        "caller": "",

        "apiKey": "",

        "sourceIp": "192.168.100.1",

        "cognitoAuthenticationType": "",

        "cognitoAuthenticationProvider": "",

        "userArn": "",

        "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",

        "user": ""

      },

      "resourcePath": "/{proxy+}",

      "httpMethod": "GET",

      "apiId": "wt6mne2s9k"

    },

    "resource": "/{proxy+}",

    "httpMethod": "GET",

    "queryStringParameters": {

      "name": "me"

    },

    "stageVariables": {

      "stageVarName": "stageVarValue"

    },

    "body": "'{\"user\":\"john\",\"pwd\":\"pwd1\"}'"

  }

請記住,body來自 API 網關的始終是字符串化的,因此如果您想訪問它,首先需要使用json.loads(event["body"]).


請記住,當返回 API Gateway 時,您的響應正文必須是字符串化的,正如我們在此答案中所討論的那樣。


您可以在文檔中看到從 API Gateway 發送的事件


查看完整回答
反對 回復 2021-12-16
?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

我從回復中得到的關鍵與他的建議不同,但他的建議對我有幫助,所以我接受它作為答案


我得到了這個回應。該body鍵來為null。但有queryStringParameters


{

  "resource": "/match_creds",

  "path": "/match_creds",

  "httpMethod": "GET",

  "headers": null,

  "multiValueHeaders": null,

  "queryStringParameters": {

    "pwd": "pwd1",

    "user": "JOHN"

  },

  "multiValueQueryStringParameters": {

    "pwd": [

      "pwd1"

    ],

    "user": [

      "JOHN"

    ]

  },

  "pathParameters": null,

  "stageVariables": null,

  "requestContext": {

    "path": "/match_creds",

    "accountId": "",

    "resourceId": "",

    "stage": "test-invoke-stage",

    "domainPrefix": "testPrefix",

    "requestId": "",

    "identity": {

      "cognitoIdentityPoolId": null,

      "cognitoIdentityId": null,

      "apiKey": "test-invoke-api-key",

      "cognitoAuthenticationType": null,

      "userArn": "",

      "apiKeyId": "test-invoke-api-key-id",

      "userAgent": "",

      "accountId": "",

      "caller": "",

      "sourceIp": "test-invoke-source-ip",

      "accessKey": "",

      "cognitoAuthenticationProvider": null,

      "user": ""

    },

    "domainName": "testPrefix.testDomainName",

    "resourcePath": "/match_creds",

    "httpMethod": "GET",

    "extendedRequestId": "",

    "apiId": ""

  },

  "body": null,

  "isBase64Encoded": false

}

我改變了我的功能


import json


    def lambda_handler(event, context):


        json_data = event["queryStringParameters"] 

        user = json_data["user"]


        return {

            "statusCode": 200,

            "headers": {"Content-Type": "application/json"},

            "body": json.dumps(user)

            }


查看完整回答
反對 回復 2021-12-16
  • 2 回答
  • 0 關注
  • 263 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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