Bent
Bent

Reputation: 1385

Convert curl POST to python requests

I am using a curl POST to test out the API of tipboard on my system. I was able to successfully send a POST to the board using:

curl https://** SECRET **/api/v0.1/** SECRET **/push -X POST -d "tile=big_value" -d "key=t_out" -d 'data={"title": "Outside Temperature", "description": "Temperature gathered by DS18B20 sensor", "big-value": "24.4 °C"}'

As the script which shall be used in the end is based on Python, I did some research and found the requests python library.

I transformed my curl request into the following code:

payload = {'title': 'big_value', 'key': 't_out', 'data': '"title": "Outside Temperature", "description": "Temperature gathered by DS18B20 sensor", "big-value": "24.4 °C"'}
r = requests.post("https://** SECRET **/api/v0.1/** SECRET **/push", data=payload)
print(r.text)
print(r.status_code, r.reason)

Which gives me the following error code:

There is no 'tile' field in your request.
(400, 'Bad Request')

I assume that my payload is not setup correctly, most likely the issue is, that I am only sending one "data" object when using Python rather than sending multiple "data elements" like with curl.

How can I equally transform my curl request into the Python variant?

Edit:

Following Dan Lowe's hint that I had a 'title' rather than 'tile' in my request lead me further with a different error using the following (edited) code:

payload = {'tile': 'big_value', 'key': 't_out', 'data': '"title": "Outside Temperature", "description": "Temperature gathered by DS18B20 sensor", "big-value": "24.4 °C"'}
r = requests.post("https://dev-01.linuxmind.de/api/v0.1/46d7213bd81d441783dc9ab77e5515c2/push", data=payload)
print(r.text)
print(r.status_code, r.reason)

Error:

Invalid JSON data.

(400, 'Bad Request')

Upvotes: 0

Views: 1187

Answers (1)

Tanu
Tanu

Reputation: 1563

Convert data object to post as string not as dict. You can do this by

data = json.dumps(payload) 

Upvotes: 1

Related Questions