New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
框架中集成文件上传功能 #169
Comments
在框架中内置 - config:
name: test upload file with httpbin
request:
base_url: https://httpbin.org
- test:
name: upload file
variable_binds:
- field_name: "file"
- file_path: "LICENSE"
- file_type: "text/html"
- multipart_encoder: ${multipart_encoder($field_name, $file_path, $file_type)}
request:
url: /post
method: POST
headers:
Content-Type: ${multipart_content_type($multipart_encoder)}
data: $multipart_encoder
validators:
- eq: ["status_code", 200]
- startswith: ["content.files.file", "MIT License"] |
使用上面代码,报错:multipart_encoder is not defined in bind @@@functions!是什么问题? - config:
name: test upload file
- test:
name: upload file
variable_binds:
- field_name: "xmlfile"
- file_path: "D:\\Dev\\httprunner\\HttpRunner-master1.4.1\\HttpRunner-master\\examples"
- file_type: "text/xml"
- multipart_encoder: ${multipart_encoder($field_name, $file_path, $file_type)}
request:
url: http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx
method: POST
headers:
Content-Type: ${multipart_content_type($multipart_encoder)}
data: $multipart_encoder
validators:
- eq: ["status_code", 200] |
@julianmail 你用的版本是? |
现在用的是1.4.1的版本 |
一个请求内上传两个文件的话data的写法应该是怎么样的呢 |
其实可以直接使用request提供的文件上传功能 - test:
name: file
variables:
- file_path: d:\code\python\worktest\test.py
- mode: 'rb'
request:
url: /
method: POST
data: #表单数据
name: jcuan
files: #文件数据
fileKey:
- test.html #文件名
- ${open($file_path,$mode)} #文件内容,通过open打开
- text/html #content-type |
@debugtalk 原来是这样,我这边是正常的 win 10
|
和楼上一样的版本一样的问题,VariableNotFound: multipart_encoder is not found |
有解决方法了吗?我也需要上传多个文件呢 |
@jcuan 麻烦问下,我的环境跟你的差不多,上传文件失败,files跟你的一样,但在前端查看请求files内容为空的 |
@ouylongzhou 我刚刚又试了一下是可以的,你没有设置content-type吧(不能设置,否则会缺少boundary)? - config:
name: local test
request:
base_url: http://127.0.0.1:8000
verify: false
headers:
User-Agent: python-requests/2.18.4
- test:
name: file
variables:
- file_path: /Users/jcuan/code/python/httprunner/file.txt
- mode: 'rb'
request:
url: /
method: POST
data:
name: jcuan
files:
fileKey:
- fileRename.txt
- ${open($file_path,$mode)}
- text/plain server收到的信息, 文本表单、文件、还有文件内容都是正常的
|
1、jcuan 说的是正解。 request:
headers:
User-Agent:Android Client
method: POST
url: http://xxx.xx.xx.xx//upload
files:
chnCode:
-
- TEST
fileDesc:
-
- HUAWEI NXT-AL10
filecontent:
- $file_name
- ${open(file.dat)}
- application/octet-stream 6、例子说明:对于非文件参数,则第一个参数留空,第二参数才是值,对于json 格式 空用null字符代替。 {
"field1": (名称, 值, 类型)
"field1": (None, "字符串值")
"field1": ("1.jpg", open("1.jpg"), "image/jpg")
} |
@hstart 使用 requests files 进行上传会存在问题,当上传文件非常大的时候,占用内容会非常高。
https://toolbelt.readthedocs.io/en/latest/uploading-data.html#streaming-multipart-data-encoder |
最终方案:https://docs.httprunner.org/prepare/upload-case/ 对于上传文件类型的测试场景,HttpRunner 集成 requests_toolbelt 实现了上传功能。 在使用之前,确保已安装如下依赖库: 使用内置 - test:
name: upload file
request:
url: http://httpbin.org/upload
method: POST
headers:
Cookie: session=AAA-BBB-CCC
upload:
file: "data/file_to_upload"
field1: "value1"
field2: "value2"
validate:
- eq: ["status_code", 200] 同时,你也可以继续使用之前描述形式(适用版本:2.0+)。 - test:
name: upload file
variables:
file: "data/file_to_upload"
field1: "value1"
field2: "value2"
m_encoder: ${multipart_encoder(file=$file, field1=$field1, field2=$field2)}
request:
url: http://httpbin.org/upload
method: POST
headers:
Content-Type: ${multipart_content_type($m_encoder)}
Cookie: session=AAA-BBB-CCC
data: $m_encoder
validate:
- eq: ["status_code", 200] |
对于文件请求方式以PUT上传oss服务器,如何处理呢? |
针对常见的文件上传场景,在框架中集成为内置功能。
#23 #103 #144
The text was updated successfully, but these errors were encountered: