『居善地』接口测试 — 5、Requests库POST请求(一)

POST请求用于向服务器提交数据,比如增删改数据,提交一个表单新建一个用户、或修改一个用户等。

对于POST请求,我们可以通过浏览器开发者工具或者其他外部工具来进行抓包,得到请求的URL、请求头(request headers)以及请求的表单data信息,这三样恰恰是我们用Requests模拟POST请求时需要的。

关于请求头的配置和GET请求是一样的,都是定义headers属性即可。

而关于POST请求提交的参数,是和GET请求是不一样的。

post请求四种传送正文方式:

  • (1)请求正文是application/x-www-form-urlencoded
  • (2)请求正文是multipart/form-data
  • (3)请求正文是raw
  • (4)请求正文是binary

这四种提交数据的方式,是在请求头Content-Type属性中来定义。

1、application/x-www-form-urlencoded

Reqeusts支持以application/x-www-form-urlencoded数据格式发送POST请求(标准的POST请求数据格式,默认),只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可。

示例:

"""
1.学习目标
    必须掌握requests库发送post请求方法
2.HTTP协议中post请求参数类型
    requests.post(url, data=None, json=None, **kwargs)
    根据不同的请求参数类型分为如下几种:
        x-www-form-data-urlencoded
        raw_json格式
        form-data
        binary
3.json格式
    # 1.导入requests库
    # 2.明确请求地址
    # 3.明确请求参数
        data = {key:value}  字典格式
    # 4.发送请求
        requests.post(url=url,json=data)
4.需求
    通过访问http://httpbin.org/post接口,验证post参数类型

"""
# 1.导入requests库
import requests
import json

# 2.明确请求地址
url = "http://httpbin.org/post"
# 3.明确请求参数
data = {
    "dep_id": "T01",
    "dep_name": "Test学院",
    "master_name": "Test-Master",
    "slogan": "Here is Slogan"
}
# 4.发送请求
response = requests.post(url=url, data=data)

# 将python对象转换为json字符串(格式化返回数据)
result = json.dumps(response.json(), indent=2, ensure_ascii=False)
# print(type(result))  # 字符串类型
print(result)

"""
返回结果:
{
  "args": {},
  "data": "",
  "files": {},
  ****************主要看这里
  "form": {
    "dep_id": "T01",
    "dep_name": "Test学院",
    "master_name": "Test-Master",
    "slogan": "Here is Slogan"
  },
  ***********************
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "88",
    *****************主要看这里
    "Content-Type": "application/x-www-form-urlencoded",
    *****************
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.18.4",
    "X-Amzn-Trace-Id": "Root=1-5ff401e3-1553596b7788e77e275c4772"
  },
  "json": null,
  "origin": "106.35.9.12",
  "url": "http://httpbin.org/post"
}
"""

说明:

  • 发送的请求中,form属性接收了参数。
  • 在请求头中,Content-Type属性为application/x-www-form-urlencoded
  • 使用application/x-www-form-urlencoded格式发送数据,requests.post(url=url, data=data)方法中一定要使用data变量来接收参数。
  • 换句话说数据格式是字典格式,使用data变量来接收,会默认发送application/x-www-form-urlencoded数据格式的POST请求。(也可以在请求头中明确一下Content-Type属性,但没必要。)

2、请求正文是raw

RAW的原意就是“未经加工”。换句话说RAW方式使用的是纯字符串的数据上传方式,所以在发送POST请求之前,可能需要手工的把一些JSON格式的数据转换成字符串的(加两单引号),在进行提交。

RAW数据格式的POST请求有两种:

  • 一种是xml格式文本(text/xml)
  • 一种是json格式文本(application/json)

下面我们一一说明:

(1)json格式文本(application/json)
# 1.导入requests库
import requests
import json

# 2.明确请求地址
url = "http://httpbin.org/post"
# 3.明确请求参数
data = {
    "data": [
        {
            "dep_id": "T01",
            "dep_name": "Test学院",
            "master_name": "Test-Master",
            "slogan": "Here is Slogan"
        }
    ]
}

# headers = {"Content-Type": "application/json"}

# 4.发送请求
response = requests.post(url=url, json=data)
print(response)  # <Response [200]>
print(response.text)


"""
返回结果:
{
  "args": {}, 
  "data": "{\"data\": [{\"dep_id\": \"T01\", \"dep_name\": \"Test\\u5b66\\u9662\", \"master_name\": \"Test-Master\", \"slogan\": \"Here is Slogan\"}]}", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Content-Length": "119", 
    **************************主要看这里
    "Content-Type": "application/json", 
    **************************
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.18.4", 
    "X-Amzn-Trace-Id": "Root=1-5ff40a9d-6a6f19d272ba4c1b40ff7bbb"
  }, 
    **************************主要看这里
  "json": {
    "data": [
      {
        "dep_id": "T01", 
        "dep_name": "Test\u5b66\u9662", 
        "master_name": "Test-Master", 
        "slogan": "Here is Slogan"
      }
    ]
  }, 
    **************************
  "origin": "106.35.9.12", 
  "url": "http://httpbin.org/post"
}
"""

说明:

  • 发送的请求中,json属性接收了参数。
  • 在请求头中,Content-Type属性为application/json
  • 使用application/json格式发送数据,requests.post(url=url, json=data)方法中一定要使用json变量来接收参数。
  • 换句话说数据格式是Json格式,使用json变量来接收,Requests会默认发送application/json数据格式的POST请求。(也可以在请求头中明确一下Content-Type属性,但没必要。)

注意:

这里我们可以发现Requests模拟post请求时,请求头格式为application/x-www-form-urlencoded与application/json的主要差别在于请求主体的构造格式(前者是键值对,后者是JSON串),前者直接用字典传入,后者用json.dumps()函数将字典转为JSON串即可。

也就是说在有需要的时候json模块下的dumps函数可以将dict转换为str。

(2)xml格式文本(text/xml)
# 1.导入requests库
import requests
import json

# 2.明确请求地址
url = "http://httpbin.org/post"
# 3.明确请求参数
data = '<sites>' \
            '<site>' \
                '<name>菜鸟教程</name>' \
                '<url>www.runoob.com</url>' \
            '</site>' \
            '<site>' \
                '<name>Google</name>' \
                '<url>www.google.com</url>' \
            '</site>' \
       '</sites>'

#  requests.post方法中适用json变量来接收数据,
# 默认是"Content-Type": "application/json", 
# 这里我们需要重新声明一下Content-Type属性。
headers = {'Content-type': 'text/xml'}

# 4.发送请求
# 如果数据用data变量来接收会报错。
response = requests.post(url=url, json=data, headers=headers)
print(response)  # <Response [200]>
# print(response.text)

# 将python对象转换为json字符串(格式化返回数据)
result = json.dumps(response.json(), indent=2, ensure_ascii=False)
# print(type(result))  # 字符串类型
print(result)

"""
返回结果:
{
  "args": {},
  "data": "\"<sites><site><name>\\u83dc\\u9e1f\\u6559\\u7a0b</name><url>www.runoob.com</url></site><site><name>Google</name><url>www.google.com</url></site></sites>\"",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Content-Length": "149",
    **************************主要看这里
    "Content-Type": "text/xml",
    **************************
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.18.4",
    "X-Amzn-Trace-Id": "Root=1-5ff40fa5-21a79b532b1ccf6d20173fd7"
  },
  **************************主要看这里
  "json": "<sites><site><name>菜鸟教程</name><url>www.runoob.com</url></site><site><name>Google</name><url>www.google.com</url></site></sites>",
    **************************
  "origin": "106.35.9.12",
  "url": "http://httpbin.org/post"
}
"""

说明:

  • text/xml格式相对用的少。
  • xml也可以作为一个文件来传输。
  • 需要重新声明请求头中Content-Type属性。
  • 其他和application/json一样。

提示:其实raw格式数据可以上传text、json、xml、html等纯字符的文本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值