import http.client
import json
connection = http.client.HTTPSConnection('api.github.com')
headers = {'Content-type': 'application/json'}
foo = {'text': 'Hello world github/linguist#1 **cool**, and #1!'}
json_foo = json.dumps(foo)
connection.request('POST', '/markdown', json_foo, headers)
response = connection.getresponse()
print(response.read().decode())
我会引导你完成它。首先,您需要创建一个用于与远程服务器通信的 TCP 连接。
>>> connection = http.client.HTTPSConnection('api.github.com')
--http.client.HTTPSConnection()
Thẹ̣n 您将需要指定请求标头。
>>> headers = {'Content-type': 'application/json'}
在这种情况下,我们说请求正文的类型为 application/json。
接下来,我们将从 python dict() 生成 json 数据
>>> foo = {'text': 'Hello world github/linguist#1 **cool**, and #1!'}
>>> json_foo = json.dumps(foo)
然后我们通过 HTTPS 连接发送一个 HTTP 请求。
>>> connection.request('POST', '/markdown', json_foo, headers)
得到响应并阅读它。
>>> response = connection.getresponse()
>>> response.read()
b'<p>Hello world github/linguist#1 <strong>cool</strong>, and #1!</p>'