首页 文章

从Python到Dynamodb的AWS Gateway API发布

提问于
浏览
0

我正在从Python向Amazon Gateway API发布一个简单的JSON结构 .

这是JSON结构:

payload = {
'ping': '1.967',
'download': '570.01',
'upload': '697.85',
'timestamp': '1527845073'
}

这是python代码:

headers = {'content-type':'application/json'}
r = requests.post("https:xxx.execute-api.us-east-1.amazonaws.com/beta/device", data = payload ,headers=headers)
print(r.status_code, r.reason)
print(r.text)
  • r.status_code返回'200'

  • r.reason返回'Ok'

  • r.text返回为{"__type":"com.amazon.coral.validate#ValidationException","message":"One or more parameter values were invalid: An AttributeValue may not contain an empty string"}

参数显然不是空的,所以我不确定为什么我会收到这个错误 . 我在Integration Request中有一个Body Mapping Template,如下所示:

{ 
"TableName": "device",
"Item": {
"id": {
        "S": "$context.requestId"
        },
"ping": {
        "S": "$input.path('$.ping')"
        },
"download": {
        "S": "$input.path('$.download')"
    },
"upload": {
        "S": "$input.path('$.upload')"
    },
"timestamp": {
       "S": "$input.path('$.timestamp')"
}
}
}

当我从AWS Gateway API控制台中运行测试时,数据输入到DynamoDB就好了 . 但是,它不适用于Python . 知道我可能做错了吗?我认为这是一个愚蠢的错误 . 谢谢 .

1 回答

  • 0

    可能需要创建JSON有效负载数据的字符串格式 . 试试这个:

    payload=json.dumps({
        'ping': '1.967',
        'download': '570.01',
        'upload': '697.85',
        'timestamp': '1527845073'
    })
    

相关问题