简体   繁体   English

Python Get / POST HTTP请求

[英]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. 我对Python的了解非常有限,但是我知道我的问题对如何发送GET / Post请求有点简单。 i'm trying to create a simple program for the (to be released LaMatric). 我正在尝试为(即将发布的LaMatric)创建一个简单的程序。 it displays info coming from a GET request on a dot matrix like screen. 它在屏幕等点矩阵上显示来自GET请求的信息。 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. 我想将其与Eventghost连接,然后能够将所有种类的信息(天气,提醒...等等)发送到屏幕。 On the website they provide you with this code to get started, but i'm not sure how to convert that to Python. 在网站上,他们为您提供了入门代码,但是我不确定如何将其转换为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)

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)

The best way to send json data is using json parameter, in words of original documentation: 用原始文档的话来说,发送json数据的最佳方法是使用json参数:

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. 除了自己对dict进行编码外,您还可以使用json参数(在2.4.2版中添加)直接传递dict,它将自动进行编码。

>>> 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 . 您可以在此处完整地查看文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM