httprunner learning 25 file upload multipart / form-data

Foreword

httprunner upload file interface, in fact, upload files with the interface requests is the same, before the python interfaces series there are cases
python interface to automate 16-multipart / form-data upload pictures

File upload multipart / form-data

With fiddler capture, view caught interface, the interface is that the multipart / form-data

  • Content-Type: multipart/form-data
  • body parameters in this format:

-----------------------------22165374713946
Content-Disposition: form-data; name="localUrl"

yoyoketang.png
-----------------------------22165374713946
Content-Disposition: form-data; name="imgFile"; filename="yoyoketang.png"
Content-Type: image/png

Python code corresponding

upload image

When uploading files with files = parameter to accept the request, the request type parameter is a dictionary, according to the fiddler to capture stitching request parameters, such as we see in the capture file parameter

Content-Disposition: form-data; name="imgFile"; filename="yoyoketang.png" Content-Type: image/png

Name = value key is then later "imgFile", value tuples is a value type (list type may be),

  • The first parameter is the file name: "xxx.jpg"
  • The second parameter is open to open the object file, such as: open ( 'filepath', 'rb')
  • The third parameter is the value corresponding to the file type rear Content-Type: image / png
 # 上海悠悠,QQ交流群:750815713
import requests

s = requests.session()  # 保持会话

# 上车文件地址
url1 = "http://127.0.0.1:81/zentao/file-ajaxUpload-5a26aca290b59.html?dir=image"

f ={
    "localUrl": (None,"1.png"),
    "imgFile": ("1.png", open("d:\\1.png", "rb"), "image/png")
  }
r = s.post(url1, files=f)
try:
    jpgurl = base+r.json()["url"]
    print(u"上传图片后的url地址:%s"%jpgurl)
except Exception as msg:
    print(u"返回值不是json格式:%s"%str(msg))
    print(r.content)

httprunner script

If the above script is determined run through, go httprunner write the script, the following is just a sample request

 # 上海悠悠,QQ交流群:750815713
-   config:
        name: 上传文件
        variables: {}
-   test:
        name: files
        request:
            files:
                imgFile: ['name35.jpg', '${get_file()}','image/jpeg']
            headers:
                Authorization: Bearer 登陆的token
                User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148
            method: POST
            url: http://x.x.x.x/path/files
        validate:
        -   eq:
            - status_code
            - 200
        -   eq:
            - headers.Content-Type
            - application/json;charset=UTF-8

get_file () method to read a file written in debugtalk.py file

# debugtalk.py

# 读取文件内容
def get_file(filePath="name35.jpg"):
    return open(filePath, "rb")

filePath you put the address of the picture, if not a directory to write the absolute path, in the same directory as the file name can be written directly

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11863610.html