Requests处理上传文件(图片、表单、附件等)

  • 前言:我们在做接口测试的时候肯定会遇到一些上传图片,然后进行校验,今天我们一起学习通过requests上传图片,查看是否上传成功
  • 下面是上传图片,抓包结果
  • 如何使用requests模拟这种场景呢?下面介绍两种方法
    • 方法1:使用requests模块POST方法
      • 首先看下官方介绍
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
if __name__ == '__main__':
    # 保持会话
    s = requests.session()
    # 登录
    login(s)

    #获取token
    url = 'http://xxxxxx/xadmin/apiapp/fileimage/'
    r = s.get(url)
    token = re.findall("name='csrfmiddlewaretoken' value='(.*?)' />",r.text)

    payload = {
        "csrfmiddlewaretoken":token[0],
        "title":"tup0729-02"
    }
    files = [
        ('image',('tu.png',open('tup.png','rb'))),
    ]

    url2 = 'http://xxxxxx/xadmin/apiapp/fileimage/add/'
    r = s.post(url2,headers = headers,data = payload,files = files)
    print(r.text)
- 思考:传多个文件的时候如下,这两个参数的name都是一样的,如果用字典去传key值,很显然python的key值是不能重复的
Content-Disposition: form-data; name="files[]"; filename="1.png"
Content-Type: image/png

Content-Disposition: form-data; name="files[]"; filename="2.png"
Content-Type: image/png

#-------------多个文件用list类型------------
file = [
    ("files[]", ("2.png", open("1.png", "rb"), "image/png")),
    ("files[]", ("2.png", open("2.png", "rb"), "image/png")),
    ]
  • 方法2:第三方模块 requests_toolbelt
    • 环境安装:pip install requests-toolbelt
# multipart/form-data传文件
from requests_toolbelt import MultipartEncoder
import requests
  
m = MultipartEncoder(
    fields={'field0': 'value',
           'field1': 'value',
            'field2': ('文件名称', open('文件地址/file.py', 'rb'), 'text/plain')}
            #"file": ("xxxx.jpg", image, "image/jpeg")    图片
    )
  
r = requests.post('http://httpbin.org/post',
                  data=m,
                  headers={'Content-Type': m.content_type})
# multipart/form-data表单提交(传非文件)
from requests_toolbelt import MultipartEncoder
import requests
  
m = MultipartEncoder(
       fields={'field0': 'value',
               'field1': 'value'})
  
r = requests.post('http://httpbin.org/post',
                  data=m,
                  headers={'Content-Type': m.content_type}) 
- fields 参数既可以传字典也可以传list(上传多个文件的时候使用list)
  - 下面直接上代码吧
from requests_toolbelt import MultipartEncoder

if __name__ == '__main__':
    # 保持会话
    s = requests.session()
    # 登录
    login(s)

    #获取token
    url = 'http://49.235.92.12:7005/xadmin/apiapp/fileimage/'
    r = s.get(url)
    token = re.findall("name='csrfmiddlewaretoken' value='(.*?)' />",r.text)

    m = MultipartEncoder(
        fields=[
            ("csrfmiddlewaretoken",token[0]),
            ("csrfmiddlewaretoken",token[0]),
            ("title", "tup1113-01"),
            ("_save", ""),
            ("files",""),
            ("image",('tup.png', open('tup.png','rb'), "image/png"))
        ],

        # fields={
        #     "csrfmiddlewaretoken": token[0],
        #     "title": "附件002",
        #     "_save": "",
        #     "files": ('tup.png', open('tup.png', 'rb'), "image/png"),
        #     "image": ""
        # }
    )
    url2 = 'http://49.235.92.12:7005/xadmin/apiapp/fileimage/add/'
    r = s.post(url2,
               data=m,
               headers = {"content-type":m.content_type}
               )
    print(r.text)
posted @ 2022-07-29 22:10  Tony_xiao  阅读(2724)  评论(0编辑  收藏  举报