如何在POST方法中从csv文件发送JSON数据? [英] How to send JSON data from csv file in POST method?
问题描述
在我的项目中,我使用Jmeter进行负载测试。
我创建了一个测试计划如下:
In my project I am using Jmeter for load testing.
I have created a test plan as follows:
- 使用具有10个唯一用户名和密码的csv文件(成功完成)登录到应用程序
- 正则表达式提取器将从响应中获取会话ID在下一个采样器中用于提交请求。 (成功提取sessionId)
-
下一个采样器将提交请求。这个采样器接受两个参数:sessionId& licenseRequest。 java脚本的一些细节如下:
- Login to the application using a csv file which has 10 unique username and password (successfully done)
- A regular expression extractor will get the session id from the response which will be used in the next sampler for submitting a request. (sessionId extracted successfully)
The next sampler will submit a request. This sampler accepts two parameters: sessionId & licenseRequest. A little details for the java-script is as follows:
url: "groovy/enqueue",
type: "POST",
global: false,
data: {sessionId: uSessionId, licenseRequest: JSON.stringify(requestJSON) },
dataType: "text",
对于提交请求,我创建了一个csv文件。
csv类似如下:
For submitting the request I have created a csv file.
The csv is similar like this:
条目1:
{ :[< activation-code>,< activation-code>],email:< emailIdofUser>,csvEntries:[{model:< modelname> serial:< serialNo"}],comment:testing jmeter}
在第二个采样器中提交请求时,post请求格式错误:
What I have found out that while submitting the request in th second sampler the post request is malformed:
POST data:
sessionId=vZNjFjW38cid&licenseRequest=%3CEOF%3E
正如您所看到的,licenseRequest的值不正确。
As you can see the licenseRequest's value is not correct. It is sending the EOF, which is not desired.
推荐答案
确保您拥有 CSV数据集 在EOF上循环
和在EOF上停止主题
值:
- 如果您计划多次迭代该文件,必须设置
Recycle on EOF = True
(即指示jmeter返回CSV文件的顶部); - 设置
EOF = False
如果你正在使用一个循环控制器,停止线程EOF =真
如果你使用一个控制器,整个csv数据集; - 如果您需要N个线程中的每一个从csv文件读取和使用单个和唯一的行,您必须设置
共享模式: CSV数据集配置的当前线程组
(在这种情况下,csv条目的数量应与线程数相同,或在EOF上循环使用? False
应该设置否则); - 如果你需要N个线程中的每一个读取和使用csv文件中的所有行,你必须设置
共享模式:当前线程
用于CSV数据集配置
- if you plan to iterate over the file more than once you must set
Recycle on EOF = True
(i.e. instruct jmeter to move back to the top of the CSV file); - set
Stop Thread on EOF = False
if you are using a loop controller,Stop Thread on EOF = True
if you are using a while controller and want to stop after reading the whole csv dataset; - in case if you need that each of N threads reads and uses single and unique line from csv-file you have to set
Sharing mode: Current thread group
for CSV Data Set Config (number of csv-entries should be in this case the same as threads number, orRecycle on EOF? False
should be set otherwise); - in case if you need that each of N threads reads and uses all lines from csv-file you have to set
Sharing mode: Current thread
for CSV Data Set Config.
请查看jmeter.log或使用日志查看器检测csv使用情况的任何问题。
Don't forget to look into jmeter.log or use Log Viewer to detect any issues with csv usage.
最简单的情况如下:
Test Group
Number of Threads = 10
CSV Data Set Config (User Logins)
Filename: ... (your csv-file should have 10 entries)
Recycle on EOF = False
Stop Thread on EOF = True
Sharing Mode = All threads
CSV Data Set Config (License Request)
Filename: ... (your csv-file should have 10 entries)
Recycle on EOF = False
Stop Thread on EOF = True
Sharing Mode = All threads
Login Sampler
License request Sampler
这将产生10个线程,每个线程都有来自csv的单独登录条目和许可证请求条目。
This will produce 10 threads each with separate login-entry and license-request-entry from csv's.
这篇关于如何在POST方法中从csv文件发送JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!