cell-in
cell-in

Reputation: 799

BeanShell Post Processor parse Json using Jmeter

I need to test rest api posting Json. I try to post Json Data by BeanShell PostProcessor using Jmeter. But BeanShell PostProcessor doesn't work or I couldn't write the right code.

My Json is : {"email":"[email protected]","password":"123"}

You can see in the pictures below what i did.

enter image description here

enter image description here

enter image description here

Upvotes: 2

Views: 10205

Answers (3)

Kayvan Tehrani
Kayvan Tehrani

Reputation: 3210

I have used Ralf Sternberg’s slim JSON parser for Java as it is described here.

This is maven dependency of it's current version:

<dependency>
     <groupId>com.eclipsesource.minimal-json</groupId>
     <artifactId>minimal-json</artifactId>
     <version>0.9.4</version>
</dependency>

Don't forget to add this library to Jemeter's lib directory.

Upvotes: 0

engincancan
engincancan

Reputation: 2482

I think you wanted to make an http request on an api. Follow this steps.

  1. Open a new instance of JMeter
  2. Under Test Plan, add a HTTP Header Manager to set the content type by navigating to Test Plan > Add > Config Element > HTTP Header Manager. Add new value for Name = “Content-Type” and value = “application/json” (without quotes).
  3. Under Test Plan, add a thread group by navigating to Test Plan > Add > Threads (Users) > Thread Group
  4. Under Thread Group, add a sampler of type HTTP Request by navigating to Thread Group > Add > Sampler > HTTP Request
  5. You can add various listeners to the Thread Group. I prefer Results in Table/Tree by navigating to Thread Group > Add > Listener > View Results in Table

You can add JSON data manully like shown on the picture.

JMeter Http Request Parameters

There is a gist also

This gist includes BeanShell Post Processor Version

Upvotes: 4

Dmitri T
Dmitri T

Reputation: 168122

in.

There is a couple of problems with your Beanshell Sampler code:

  1. In Beanshell you need to escape quotation marks with slash like \"
  2. SampleResult.setResponseData method accepts byte array, not string.

So if you amend your Beanshell Sampler code as follows:

String dummyJson = "{\"email\":\"[email protected]\",\"password\":\"123\"}";
SampleResult.setResponseData(dummyJson.getBytes("UTF-8"));

everything should be fine from Beanshell perspective.

See How to use BeanShell: JMeter's favorite built-in component guide for more details on Beanshell scripting in Apache JMeter.

Upvotes: 1

Related Questions