2

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":"selin@xxx.com","password":"123"}

You can see in the pictures below what i did.

enter image description here

enter image description here

enter image description here

cell-in
  • 709
  • 2
  • 11
  • 27

3 Answers3

4

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

engincancan
  • 2,442
  • 2
  • 28
  • 43
1

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\":\"selin@xxx.com\",\"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.

engincancan
  • 2,442
  • 2
  • 28
  • 43
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thank you. I fixed BeanShell sampler code like you said. But i am still getting error, I think my BeanShell PostPocessor code has problem. – cell-in Jul 07 '14 at 14:24
  • I don't know the specifics of what you need to send, but. 1. where `getJSONObject` method is defined? 2. Your "Dummy Post Request" server IP/Name section is empty. 3. Path should start from `/` – Dmitri T Jul 07 '14 at 14:42
0

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.

Kayvan Tehrani
  • 3,070
  • 2
  • 32
  • 46