Python post请求两种传送正文方式

2019-08-22  本文已影响0人  maximaximi

HTTP 请求中 POST 提交的数据常见的两种编码方式:

1、application/x-www-form-urlencoded 

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

key_1中argument以参数传递进去

d = {'key_1': argument, 'key_2':2,  'key_3':''}   

header = {'content-type':'application/x-www-form-urlencoded'}

请求格式如下:

r = requests.post(url, headers=header, cookies=cookie, data=d)

r = r.json()  获取返回结果

print (r) 打印结果,查看请求是否成功

2、application/json 

header = {'content-type':'application/json'}

application/json 将json串传给requests.post()的json参数

j = {

    "key_1": 1,

    "key_2": 2,

    "key_3": ""

      }

r = requests.post(url, headers=header, cookies=cookie, json=j)

r = r.json() 

print (r) 打印结果,查看请求是否成功


上一篇 下一篇

猜你喜欢

热点阅读