javajsonjmeterweb-api-testingpost-processing

How to save json response in jmeter?


Goal: Save the response I am getting from api request to json files. filename needed: name_of_original_file_request_was_sent_with + "_response.json" Until now the jmeter program reads like 6 files, takes data from each file and puts it in body while making API request. I get 6 responses. Now How do I save those responses in files? I used this code but it replaces the original files that I sent request with:

new File(vars.get('file')).bytes = prev.getResponseData()

Below is response file:

{
  "type": "transaction-response",
  "link": [
    {
      "relation": "self",
      "url": "https://someurl.com"
    }
  ],
  "entry": [
    {
      "response": {
        "status": "201 Created",
        "location": "Player/aerear",
        "etag": "1",
        "lastModified": "2020"
      }
    }       
  ]
}

Solution

  • So you basically need to amend your code in order to:

    1. Remove extension from the original file
    2. Add _response.json to it

    For point 1 you can use FileNameUtils.getBaseName() function

    For point 2 you can use simple string concatenation

    Something like:

    new File(org.apache.commons.io.FilenameUtils.getBaseName(vars.get('file')) + '_response.json').bytes = prev.getResponseData()
    

    should do the trick for you.

    More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?