user1303
user1303

Reputation: 1

Python Get/POST http request

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)

Upvotes: 0

Views: 817

Answers (2)

Juan Antonio
Juan Antonio

Reputation: 2614

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.

Upvotes: 0

heinst
heinst

Reputation: 8786

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)

Upvotes: 1

Related Questions