来,先上代码:
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 来测试结果。
---