![](/img/trans.png)
[英]How do I get the body of a http response from a string containing the entire response, in Python?
[英]How do I get an API response containing body, but no headers with Python & AWS Lambda?
我在 Python 中使用 AWS Lambda 創建了一個 API。 不幸的是,響應包含標題,我希望它只包含正文。 Lambda API 如下所示:
import json
import boto3
def lambda_handler(event, context):
#Step1: Scan table
client = boto3.resource('dynamodb') #Access DynamoDB
table = client.Table("register-to-event") #Access table
response = table.scan()
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json",
},
"body": response["Items"]
}
問題是 API 響應在我調用它時包含標頭和正文。 這是回應:
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
{
"your-email": "hannes.hannesen@googlemail.com",
"your-number": "004785454548",
"your-last-name": "Hannesen",
"your-first-name": "Hannes",
"ID": 3
},
{
"your-email": "stig.stigesen@googlemail.com",
"your-number": "+4754875456",
"your-last-name": "Stigesen",
"your-first-name": "Stig",
"ID": 0
}
]
}
目標是調用 API 並僅返回 json主體,如下所示:
[
{
"your-email": "hannes.hannesen@googlemail.com",
"your-number": "004785454548",
"your-last-name": "Hannesen",
"your-first-name": "Hannes",
"ID": 3
},
{
"your-email": "stig.stigesen@googlemail.com",
"your-number": "+4754875456",
"your-last-name": "Stigesen",
"your-first-name": "Stig",
"ID": 0
}
]
解決方案是通過勾選“使用 Lambda 代理集成”旁邊的框來正確配置 AWS 中的 API:使用 Lambda 代理集成
我的下一個問題最終是“序列化小數”,因為 DynamoDB 使表的鍵成為 Python 中不存在的類型(十進制)。 可以在此處找到解決方案: https : //learn-to-code.workshop.aws/persisting_data/dynamodb/step-3.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.