python接口自动化15-multipart/form-data表单提交

前言

multipart/form-data这种格式官方文档给的参考案例比较简单,实际情况中遇到会比较复杂,本篇讲解multipart/form-data的表单如何提交,非图片上传

一、 禅道提交bug

1.以禅道提交bug为参考案例,手动操作一次提交bug,抓包查看Content-Type: multipart/form-data

2.在WebForms里面查看参数详情

二、参数填写

1.注意了,这里的参数对应为字典格式,字典的key对应WebForms里查看的name="这里的参数",value对应WebForms查看的value值

```
f = {
    "product": "1",
    "module": "0",
    "project": "",
    "openedBuild[]": "trunk",
    "assignedTo": "admin",
    "type": "codeerror",
    "os": "all",
    "browser": "all",
    "color": "",
    "title": "yoyoketang-这是一个bug描述1122",
    "severity": "3",
    "pri": "0",
    "steps": '<p>[步骤]</p>\
            <p>1、第一步点</p>\
            <p>2、第二步点</p>\
            <p>3、点三步点</p>\
            <p>[结果]</p>\
            <p><img src="data/upload/1/201712/072254170557cdn.png" alt="" /></p>\
            <p>[期望]</p>',
    "story": "0",
    "task": "0",
    "mailto[]": "",
    "keywords": "",
    "files[]": "",
    "labels[]": "",
    "uid": "5a2955c884f98",
    "case": "0",
    "caseVersion": "0",
    "result": "0",
    "testtask": "0"
    }
```
2.如果有图片上传,这里<img src="data/upload/1/201712/072254170557cdn.png" 这个图片地址就是上一篇讲到的上传图片接口返回的图片地址(相对路径)

三、参考代码

1.multipart/form-data这里传的是data参数(上一篇文件上传是files参数)
2.这里头部不要加Content-Type: multipart/form-data这个参数,会报错

```
# coding:utf-8
import requests

base = 'http://127.0.0.1:81/'  # 禅道的服务器地址

loginUrl = base+"/zentao/user-login.html"

h = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
    "Accept-Encoding": "gzip, deflate",
    "Referer": "http://127.0.0.1/zentao/user-login.html",
    # "Cookie":  # 头部没登录前不用传cookie,因为这里cookie就是保持登录的
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    }

body = {"account":"admin",
        "password":"e10adc3949ba59abbe56e057f20f883e",
        "keepLogin[]":"on",
        "referer":"http://127.0.0.1/zentao/my/"
        }

s = requests.session()  # 保持会话
r = s.post(loginUrl, data=body, headers=h)
print r.content  # 打印结果看到location='http://127.0.0.1/zentao/my/'说明登录成功了

# 提交bug
url1 = "http://127.0.0.1:81/zentao/bug-create-1-0-moduleID=0.html"
f = {
    "product": "1",
    "module": "0",
    "project": "",
    "openedBuild[]": "trunk",
    "assignedTo": "admin",
    "type": "codeerror",
    "os": "all",
    "browser": "all",
    "color": "",
    "title": "yoyoketang-这是一个bug描述1122",
    "severity": "3",
    "pri": "0",
    "steps": '<p>[步骤]</p>\
            <p>1、第一步点</p>\
            <p>2、第二步点</p>\
            <p>3、点三步点</p>\
            <p>[结果]</p>\
            <p><img src="data/upload/1/201712/072254170557cdn.png" alt="" /></p>\
            <p>[期望]</p>',
    "story": "0",
    "task": "0",
    "mailto[]": "",
    "keywords": "",
    "files[]": "",
    "labels[]": "",
    "uid": "5a2955c884f98",
    "case": "0",
    "caseVersion": "0",
    "result": "0",
    "testtask": "0"
    }
r = s.post(url1, data=f)
print r.content
```

(0)

相关推荐