0

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')
asked May 22, 2016 at 14:48
6
  • 1
    Your curl code sets tile=big_value but in the Python code you have it as title, not tile. Commented May 22, 2016 at 14:50
  • Your data parameter needs to be another python dictionary, not a string Commented May 22, 2016 at 14:52
  • 1
    Take a look at this answer. stackoverflow.com/a/29687915/2308683 Commented May 22, 2016 at 14:55
  • @DanLowe fixed this issue, please see edited question. Commented May 22, 2016 at 14:57
  • @cricket_007 works like a charm, but that's almost like cheating ;) Thanks though Commented May 22, 2016 at 15:00

1 Answer 1

1

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

data = json.dumps(payload) 
answered May 22, 2016 at 15:02
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.