文件上传接口测试

一、辨别是否为上传文件接口

1. 关注请求头中content-type 字段,一般为multipart、boundary

2. 请求体中包含:name、filename

 

练习示例:

#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

# @Author  : Tina Yu
# @Time    : 2022-4-24 21:26
import requests


class TestUploadFileDemo:
    proxy = {"http": "http://127.0.0.1:8888",
             "https": "http://127.0.0.1:8888"}

    def test_upload_file_demo(self):
        # files 参数用来解决文件上传
        r = requests.post("https://httpbin.testing-studio.com/post",
                          # files={"file_key": open("1.text", "rb")},
                          # 改用 tuple 格式,达到指定文件名称的目的
                          files={"file_key": ("file_name.txt", open("1.text", "rb"))},
                          # 通过 proxies 传递代理配置,设置代理方便抓包查看过程
                          proxies=self.proxy,
                          # False 代表不会验证证书
                          verify=False
                          )
        print(r.json())
        assert r.status_code == 200

 

posted @ 2022-04-24 22:08  于慧妃  阅读(600)  评论(0编辑  收藏  举报