在JMeter中使用JSON搜索查询和GET请求

时间:2016-05-17 15:23:20

标签: rest jmeter automated-tests

我的组织正在尝试使用JMeter作为测试自动化套件的一部分来测试一些后端REST API。目前,其中一个支持使用JSON查询作为从GET请求中获取过滤结果的方法。

我们正在使用JMeter UI来创建这些测试,因为所有其他API调用都在HttpClient3.1 HTTP请求实现下工作,这是我目前用来实现此功能的实现。通过这个实现,我在查看结果树中的失败时得到以下信息(响应数据部分)我已经使错误略微更通用以保护一些IP:

  

java.lang.IllegalArgumentException:无效的uri'https://server:port/restservice/v1/users?firstname_query= {“in”:[“User1FirstName”,“User2FirstName”]}':无效的查询       在org.apache.commons.httpclient.HttpMethodBase。(HttpMethodBase.java:222)       在org.apache.commons.httpclient.methods.GetMethod。(GetMethod.java:89)       at org.apache.jmeter.protocol.http.sampler.HTTPHC3Impl.sample(HTTPHC3Impl.java:229)       at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)       at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1146)       at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1135)       在org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)       在org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)       在java.lang.Thread.run(未知来源)

我也使用Java实现尝试了相同的请求并获得了类似的结果。

如果有任何想法或您需要更多信息,请告诉我,并提前再次感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

您的HTTP请求配置有点不正确。 JSON实体包含大括号{},这些大括号在URL中不允许,因此您需要对它们进行编码。

配置您的HTTP Request采样器,如:

HTTP Request JSON

确保"编码"勾选方框。

这样您就可以使用HTTP GET请求发送JSON。您可以使用View Results Tree侦听器来检查请求和响应详细信息:

View Results Tree

您可能还需要将HTTP Header Manager添加到测试计划中,并将其配置为发送Content-Type标头,其值为application/json。有关正确配置JMeter以测试REST API的更多详细信息,请参阅Testing SOAP/REST Web Services Using JMeter文章。