0
votes

I'm posting a simple JSON structure to Amazon Gateway API from Python.

Here's the JSON structure:

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

And here's the python code:

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 is returned as '200'
  • r.reason is returned as 'Ok'
  • r.text is returned as {"__type":"com.amazon.coral.validate#ValidationException","message":"One or more parameter values were invalid: An AttributeValue may not contain an empty string"}

The parameters are clearly not empty so I'm not sure why I'm getting that error. I have a Body Mapping Template in the Integration Request that looks like this:

{ 
"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')"
}
}
}

And when I run the test from within AWS Gateway API Console, the data inputs into the DynamoDB just fine. However, it won't work from Python. Any idea what I could be doing wrong? I'm betting it's a silly mistake. Thank you.

1
can you change the data = payload to json = payload? Please try this. The json parameter is available since version 2.4.2 (requests package) - notionquest
Thanks! No dice: TypeError: request() got an unexpected keyword argument 'json' - Gene Ellis

1 Answers

0
votes

May be you need to create a string format of your JSON payload data. Try this:

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