Páginas

SyntaxHighlighter

terça-feira, 22 de março de 2011

JMeter - POSTing / Sending JSON data

Recently I was load testing a RESTful mobile API where all the transmitted data were encoded as JSON. It took me some time to figure out how to simulate that in JMeter using a regular HTTPRequest sampler so I decided to share it with others.

NOTE: I successfuly used this technique for webservices exposed by the Jersey library as well as ASP.NET asmx webservices

It's quite simple. Assuming you need to send POST request to the /login URI passing two parameters: email and password encoded as JSON, like this:

{ email: 'myemail@email.com', password: 'mypassword' }

Just follow two basic steps:

Step 1 - Setting the "ContentType" http header attribute to "application/json"
  • Add an HTTP Header Manager to your test plan;
  • Add a new attribute: set name to "Content-Type" and value to "application/json"



Step 2 - Setting the JSON object as an unnamed http request parameter
  • Add a HttpRequest sampler
  • Add a new parameter, leave the name blank and set the value to the JSON string:  { 'email': 'myemail@email.com', 'password': 'mypassword' }

And last but not least: you can use variables and properties as you wish in the JSON string. Suppose you have a CSV Data Set Config which defines to external variables: email e pwd. You could use them in your sampler like this:

{ email: '${email}', password: '${pwd}' }