1、以form形式发送post请求:
“Content-Type”: “application/x-www-form-urlencoded”
Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data
参数即可。
url = 'http://httpbin.org/post' d = {'key1': 'value1', 'key2': 'value2','age':18,'list':'[]'} r = requests.post(url, data=d)
2、 以json形式发送post请求:
可以将一json串传给requests.post()的data参数
“Content-Type”: “application/json”
url = 'http://httpbin.org/post' s = json.dumps({'key1': 'value1', 'key2': 'value2'}) r = requests.post(url, data=s)
3、以multipart形式发送post请求:
Requests也支持以multipart形式发送post请求,只需将一文件传给requests.post()的files
参数即可。
“Content-Type”: “multipart/form-data; boundary=467e443f4c3d403c8559e2ebd009bf4a”
url = 'http://httpbin.org/post' files = {'file': open('report.txt', 'rb')} r = requests.post(url, files=files)
发送复杂的请求,如列表中又套字典等问题:
如下图:commodity_list所对应的就是[{json}]
格式的问题,commodity_list的值中 需要去除空格,可以采用commodity_list='[%s]'%json.dumps(dic)来去除列表中的字典空格。