使用JMeter将文件上传到Rest API [英] Uploading file to rest API using JMeter

查看:757
本文介绍了使用JMeter将文件上传到Rest API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意-我已经检查过 BlazeMeter教程在使用文件上传"标签时将文档作为正文数据上传.

Note - I have checked BlazeMeter Tutorial which uploads doc as Body Data while I use File Upload tab.

这是我的请求的样子-

执行后,我得到以下请求-

On execution I get following Request -

POST https://xxx

POST data:
<actual file content, not shown here>

[no cookies]

Request Headers:
Connection: keep-alive
Content-Type: multipart/form-data
Accept-Language: en-US
Authorization: bearer <>
Accept: application/json
Content-Length: 78920
Host: test-host
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_102)

请求失败并显示400错误-

And the request fails with 400 error -

Response code: 400
Response message: Bad Request

由于我可以使用curl进行文件上传,因此我假设我错过了JMeter的某些配置.卷毛外观为-

Since I am able to carry out file upload using curl, I assume that I missed the some configuration with JMeter. Curl looks as -

curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' --header 'Authorization: Bearer <>' -F upload_file=@"test.pdf"  'https://xxx'

我在JMeter文件上传中错过了什么?

What did I miss in JMeter file upload?

推荐答案

如果您可以通过curl成功上传文件,为什么不直接通过

If you can successfully upload file via curl, why don't you just record the upload through JMeter HTTP(S) Test Script Recorder like:

curl -x http://localhost:8888 -X POST --header 'Content-Type....."


如果您仍然需要手动构建请求,请考虑以下两个重要方面:


If you still need to build the request manually consider two important bits:

  1. 您需要检查Use multipart/form-data for POST.

最重要的是,根据 HTTP请求采样器手册:

对于POST和PUT方法,如果没有要发送的文件,并且省略了参数的名称,则通过将所有参数值进行级联来创建主体.

For the POST and PUT method, if there is no file to send, and the name(s) of the parameter(s) are omitted, then the body is created by concatenating all the value(s) of the parameters.

查看 curl命令手册,在这种情况下,参数名称"应为upload_file

Looking into curl command manual in your case the "Parameter Name" should be upload_file

因此最终配置应如下所示:

So the final configuration should look like:

请参见性能测试:上传并使用Apache JMeter下载场景指南,详细说明了上述步骤.

See Performance Testing: Upload and Download Scenarios with Apache JMeter guide for above steps described in details.

这篇关于使用JMeter将文件上传到Rest API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆