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}' }

5 comentários:

  1. Thanks for these tips!

    Luckily since the time of your post, the JMeter community has addressed this issue, and from the help file we now have:

    Since JMeter 2.6, you have the option to switch to Post Body when a request has only unnamed parameters (or no parameters at all). This option is useful in the following cases (amongst others):
    GWT RPC HTTP Request
    JSON REST HTTP Request
    XML REST HTTP Request
    SOAP HTTP Request

    Hope this helps anyone who finds this post.

    ResponderExcluir
    Respostas
    1. You are right!
      I'm currently using this feature in 2.9!
      Thanks for the heads up!

      Excluir
  2. Hi Fabio,

    I am working on RESTful Services and I am using a huge json file in my RAW POST body and every time it takes very long time to come out of the node. The jmeter becomes very slow. Do we have any alternative to send the body which containes a json requests in a file.

    I use HTTP Request Sampler for REST Services.

    Any help is highly appreciated.

    ResponderExcluir
    Respostas
    1. Does your JSON has variable placeholders? Or is it static?

      Excluir