前言
multipart/form-data这种格式官方文档给的参考案例比较简单,实际情况中遇到会比较复杂,本篇讲解multipart/form-data的表单如何提交,非图片上传。
禅道提交bug
1.以禅道提交bug为参考案例,手动操作一次提交bug,抓包查看Content-Type:multipart/form-data
2.在webForms里面查看参数详情
参数填写
1.注意了,这里的参数对应为字典格式,字典的key对应WebForms里查看的name=“这里的参数”,value对应WebForms查看的value值。
bd={
"product":"6",
"module":"0",
"project":"5",
"openedBuild[]":"trunk",
"assignedTo":"huyongqin",
"type":"codeerror",
"title":"测试8866",
"severity":"1",
"pri":"2",
"steps":"
[步骤]打开浏览器;
[结果]打不开
[期望]可以打开
",
"oldTaskID":"0",
"uid":"5f2b6ce9192a9",
"case":"0"
}
参考代码
1.multipart/form-data这里传的是data参数。
2.请求头部不要加Content-Type:multipart/form-data这个参数,加了执行不会成功。
# coding:utf-8
import requests
import re
import hashlib
pw="P@ssw0rd"
s=requests.Session()
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36"
}
vrand=0
while(True):
rs1=s.get("http://localhost/zentaopms/www/user-login.html",headers=headers)
rs1.encoding='utf-8'
#print(rs1.text)
rand=re.findall(r"'verifyRand' value='(.+?)'",rs1.text)
#print(rand[0])
if len(rand[0])==10:
vrand=rand[0]
break
print(vrand)
#方式一
hash=hashlib.md5()
hash.update(pw.encode('utf-8'))
f=hash.hexdigest()+vrand
#print(f)
#方式二
hash2=hashlib.md5(f.encode('utf-8'))
pwd=hash2.hexdigest()
print(pwd)
data={
"account":"admin",
"password":pwd,
"referer":"http://localhost/zentaopms/www/bug-browse-6.html",
"verifyRand":vrand
}
rs2=s.post("http://localhost/zentaopms/www/user-login.html",headers=headers,data=data)
rs2.encoding='utf-8'
#print(rs2.text)
rs3=s.get("http://localhost/zentaopms/www/bug-browse-6.html",headers=headers)
rs3.encoding='utf-8'
#print(rs3.text)
result=re.findall(r"\(.+?)\",rs3.text)
print(result)
if result[0]=="退出":
print("登录成功")
#提交bug
burl="http://localhost/zentaopms/www/bug-create-6-0-moduleID=0.html"
bd={
"product":"6",
"module":"0",
"project":"5",
"openedBuild[]":"trunk",
"assignedTo":"huyongqin",
"type":"codeerror",
"title":"测试8866",
"severity":"1",
"pri":"2",
"steps":"
[步骤]打开浏览器;
[结果]打不开
[期望]可以打开
",
"oldTaskID":"0",
"uid":"5f2b6ce9192a9",
"case":"0"
}
rp=s.post(burl,data=bd)
rp.encoding="utf-8"
print(rp.text)
运行结果:
查看禅道系统,发现有刚刚提交的bug。