If necessary submit JSON data when Python post - Solutions

来,先上代码:
Import Requests
 Import JSON 

URL = " http://example.com " 
Data = {
     ' TT ' : 1 ,
     ' GG ' : 2 , 
} 

# The following two: 
# 1. data transfer parameters 
requests.post (URL, data = json.dumps (data))
 # 2. json parameters passed 
requests.post (url, json = data)

完。

---
知识点1:
Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可。

知识点2:
Requests也支持以multipart形式发送post请求,只需将一文件传给requests.post()的files参数即可。
url = 'http://httpbin.org/post'
files = {'file': open('report.txt', 'rb')} 
r = requests.post(url, files=files)
print r.text

 

知识点3: 简单 测试 post 的服务器 ,可以用 httpbin.org 来测试结果。

---

Guess you like

Origin www.cnblogs.com/iameric/p/12330115.html