json 在 Jmeter 中,如果我使用 Rest API 并从 CSV 文件中获取输入,那么 HTTP 请求采样器的正文数据部分中参数的语法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27634885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:41:28  来源:igfitidea点击:

In Jmeter, What would be syntax of parameters in Body Data section of HTTP Request Sampler, if i am using Rest APIs and taking input from CSV files?

jsonrestjmeterperformance-testingload-testing

提问by abhishek

I am trying to create 10 users at the same time in Jmeter, using REST APIs. Test Data i.e. input is taken from CSV files (The details of users like user name, last name , mobile)

我正在尝试使用 REST API 在 Jmeter 中同时创建 10 个用户。测试数据即输入取自 CSV 文件(用户的详细信息,如用户名、姓氏、手机)

For this I have added one thread group,User Defined Variable,HTTP Header Manager, HTTP Request Sampler (which perform successful login into web application), JSON PATH EXTRACTOR ( to extract auth token, which will be used while adding user)

为此,我添加了一个线程组、用户定义的变量、HTTP 标头管理器、HTTP 请求采样器(执行成功登录到 Web 应用程序)、JSON PATH EXTRACTOR(提取身份验证令牌,将在添加用户时使用)

To Add User, I have used one "Loop Controller" - "HTTP Request Sampler" - HTTP Header Manager, CSV Data Set Config.

为了添加用户,我使用了一个“循环控制器”-“HTTP 请求采样器”-HTTP 标头管理器、CSV 数据集配置。

And to track result, View Results Tree and Aggregate Report.

并跟踪结果,查看结果树和汇总报告。

Please tell me what would be the syntax of parameters which I am passing in "Body Data" section of HTTP Request sampler, so that it can read values from csv files.

请告诉我我在 HTTP 请求采样器的“正文数据”部分传递的参数的语法是什么,以便它可以从 csv 文件中读取值。

Below is the syntax I am using now to add single user.

下面是我现在用来添加单个用户的语法。

{
  "phoneNo": "9998885551",
  "lastName": "john25",
  "email": "[email protected]",
  "firstName": "ricky25",
  "mobileNo": "9820420420"
}

回答by Dmitri T

If your CSV file looks like:

如果您的 CSV 文件如下所示:

9998885551,john25,[email protected],ricky25,9820420420
9998885552,john26,[email protected],ricky26,9820420421
....

Configuration should be the following:

配置应该如下:

  • Filename: full path to your source .csv file
  • Variable Names: phone,lastname,email,firstname,mobile
  • Delimiter: ,
  • 文件名:源 .csv 文件的完整路径
  • 变量名称: phone,lastname,email,firstname,mobile
  • 分隔符: ,

Populate other values according to your test scenario i.e. whether you want test to stop on .csv file end or re-spin or whatever.

根据您的测试场景填充其他值,即您是否希望测试在 .csv 文件结束时停止或重新旋转或其他任何内容。

CSV Config

CSV 配置

And your HTTP Request should look like:

您的 HTTP 请求应如下所示:

Using CSV Variables in HTTP Request

在 HTTP 请求中使用 CSV 变量

The main point is that variables defined in the CSV Data Set Config need to match the ones in the HTTP Request.

重点是CSV数据集配置中定义的变量需要与HTTP请求中的变量相匹配。

See Using CSV DATA SET CONFIGguide for detailed instructions.

有关详细说明,请参阅使用 CSV 数据集配置指南。

I would also recommend adding the following entry to your HTTP Header Manager:

我还建议将以下条目添加到您的 HTTP 标头管理器中:

  • Name: Content-Type
  • Value: application/json
  • 姓名: Content-Type
  • 价值: application/json

elsewise your request will be treated like plain text and may fail.

否则您的请求将被视为纯文本并且可能会失败。

Also don't forget to disable View Results Tree listener for actual load test run as it consumes a lot of resources and may lead to out-of-memory errors on JMeter side.

另外不要忘记为实际负载测试运行禁用查看结果树侦听器,因为它消耗大量资源并可能导致 JMeter 端出现内存不足错误。

回答by JmeterQuestion

Also,if your formparam\ Body data contains multiple parameters like this:

此外,如果您的 formparam\ Body 数据包含多个参数,如下所示:

UserName=abc password=abc123,

UserName=abc password=abc123,

Instead of created two columns for userName and password, it can be given in single column like this:

不是为 userName 和 password 创建两列,而是可以像这样在单列中给出:

UserName=abc&password=abc123

UserName=abc&password=abc123

This way you can loop through multiple\different URL's which takes different numbers of bodydata

通过这种方式,您可以遍历多个\不同的 URL,这些 URL 需要不同数量的 bodydata

E.g.: If URL1 takes only username as body data and URL2 takes Username & password as body data the above will be an easy solution.

例如:如果 URL1 仅将用户名作为正文数据,而 URL2 将用户名和密码作为正文数据,则上述解决方案将是一个简单的解决方案。