Thursday 22 November 2018

Testing API with HTTP requests containg sizable JSON body


          Testing APIs with big size JSON can be troublesome. Sometimes the easiest way to test our API is to be able to get a pregenerated JSON from a separate file and send it to the API. Keeping JSON samples for testing in a file can also make it easier for us to segragate them and have a well maintained collection of them.

Using a text file with JSON for testing an API is not always straight forward. Here are 2 approaches I've used to use a big JSON file (~60MB) to test an API.

CURL

One option is to use CURL, which I've already described in: http://simonkatanski.blogspot.com/2018/10/testing-net-core-with-jmeter-and-curl.html
However it takes a bit of time, CURL prints the response, which also is not ideal in most situations.

JMeter

To send requests using JMeter and loading body from file we need to do the following:
  • Create a HTTP Header Manager, we add all the headers in it
  • Create a HTTP Request we set it as POST, set the Server Name, Port Number
  • In Body Data tab of the HTTP Request we enter the following:
                       ${__FileToString(C:\\JsonSamples\\big.json,,)}
  • Now we can add the listeners to view the results and we are done

It's also possible to use an incrementing parameter in the file name and load different files with different requests.

No comments:

Post a Comment