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

Python022

Python post请求两种传送正文方式,第1张

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) 打印结果,查看请求是否成功

一、安装:pip install requests

二、基本概念

1、POST方法

通过 POST 发送到服务器的数据存储在 HTTP 请求的请求主体中:

2、get方法

查询字符串(名称/值对)是在 GET 请求的 URL 中发送的:

3、比较 GET 与 POST

下面的表格比较了两种 HTTP 方法:GET 和 POST。