一、常用接口请求体
| 类型 | 
描述 | 
content-type | 
| JSON(JavaScript object notation) | 
轻量级的数据交换格式,最常见的一种类型 | 
application/json | 
| 表单数据(form data) | 
以键值对的形式提交的数据 | 
application/x-www-form-urlencoded | 
| xml(extensible markup language) | 
常用的标记语言,常用于传递配置文件等数据 | 
application/xmltext/xml | 
| 文件(file) | 
可以通过请求体上传文件数据,如图片、Excel。 | 
上传文件的MIME类型,例如image/jpeg | 
| 纯文本(text) | 
纯文本数据,如发送邮件、消息等 | 
text/plain | 
| 其他格式 | 
二进制数据,protobuf等格式 | 
 | 
 
二、文件上传
2.1、文件上传接口场景
- 文件上传是一种常见的需要求,但是使用 html 中的 form 表单格式却不支持,
 
- 提出了一种兼容此需求的 mime type。
 
- 解决接口测试流程中文件上传的问题
- 指定 name
 
- 指定 filename
 
- 指定 content-type
 
 
2.2、文件上传示例
def test_file_upload():
    url = "https://httpbin.ceshiren.com/post"
    proxies = {
        "http": "http://127.0.0.1:8888",
        "https": "http://127.0.0.1:8888"
    }
    with open("../../logs/2023-10-15.log", "rb") as f_obj:
        file = {"hogwarts": ("api.py", f_obj)}
        res = requests.post(url, files=file, proxies=proxies, verify=False)
        print(res.text)

三、form表单
3.1、什么是 FORM 请求
3.2、form表单使用示例
    def test_form_data(self):
        url = "https://httpbin.ceshiren.com/post"
        data = {"name": "钟离", "country": "璃月"}
        res = requests.post(url, data=data, proxies=self.proxies, verify=False)
        print(res.text)

四、xml请求体
4.1、xml数据解析
- 数据保存:将复杂的xml或者json保存到文件模版中
 
- 数据处理:
- 使用mustache、freemaker等工具解析
 
- 使用简单的字符串替换
 
- 使用json、xml进行结构解析
 
 
- 数据生成:输出最终结果
 
xml请求示例
    def test_with_xml(self):
        url = "https://httpbin.ceshiren.com/post"
        xml = """
        <?xml version='1.0' encoding='utf-8'?>
        <a>baidu.com</a>
        """
        header = {"Content-Type": 'application/xml'}
        res = requests.post(url, headers=header, data=xml, proxies=self.proxies, verify=False)
        print(res.text)
