文件上传接口测试

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

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 @   于慧妃  阅读(607)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

喜欢请打赏

扫描二维码打赏

支付宝打赏

点击右上角即可分享
微信分享提示