0

my knowledge of Python is very limited however i know my question is a bit simple on how to send a GET/Post request. i'm trying to create a simple program for the (to be released LaMatric). it displays info coming from a GET request on a dot matrix like screen. I would like to connect it with Eventghost then be able to send all kinda of info (weather, reminders.... and so on) to the screen. On the website they provide you with this code to get started, but i'm not sure how to convert that to Python.

curl -X POST \
-H "Accept: application/json" \
-H "X-Access-Token: <MY TOKEN>" \
-H "Cache-Control: no-cache" \
-d '{
    "frames": [
        {
            "index": 0,
            "text": "<TEXT GOES HERE>",
            "icon": null
        }
    ]
}' \
https://developer.lametric.com......(API)
  • 2
    [request](http://www.python-requests.org/en/latest/) would save your life. lol – luoluo Sep 01 '15 at 13:38
  • The [`requests`](http://www.python-requests.org/en/latest/) library will be of great use to you. – kylieCatt Sep 01 '15 at 13:38
  • possible duplicate of [Making a POST call instead of GET using urllib2](http://stackoverflow.com/questions/6348499/making-a-post-call-instead-of-get-using-urllib2) – Peter Wood Sep 01 '15 at 13:39

2 Answers2

1

It would look something like:

import requests

headers = {
    'Accept': 'application/json',
    'X-Access-Token': 'TOKEN',
    'Cache-Control': 'no-cache'
}

payload = {
    "frames": [
        {
            "index": 0,
            "text": "<TEXT GOES HERE>",
            "icon": "null"
        }
    ]
}

requests.post('https://developer.lametric.com......', headers=headers, data=payload)
heinst
  • 8,520
  • 7
  • 41
  • 77
  • unfortunately this is not working, and i cant figure out why, if there a different way of writing it? – user1303 Oct 09 '15 at 14:26
0

The best way to send json data is using json parameter, in words of original documentation:

Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically.

>>> url = 'https://api.github.com/some/endpoint'
>>> payload = {'some': 'data'}
>>> r = requests.post(url, json=payload)

It's very easy to use. you can see documentation completely here.

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34