omalyutin
omalyutin

Reputation: 454

JMeter Modify the GET response to use in PUT

I have two requests, the first one is GET, second is PUT. I should receive the response data from GET request, modify it a little bit and send with PUT request. So far I managed to do all, except modifying the response data.

For GET request I use Regular Expression Extractor as Preprocessor: parsing get

And currently I send the PUT request without modifying the data: sending put

JSON structure:

{
  "property1" : 1,
  "property2" : "2",
  "innerPropery" : {
    "innerProperty1" : "value1",
    "innerProperty2" : "value2",
    "innerProperty3" : "value3"
  }
}

I should change the innerProperty2.

Thanks!

Upvotes: 1

Views: 1384

Answers (2)

Dmitri T
Dmitri T

Reputation: 168157

You can do it without Regular Expression Extractor interim step.

  1. Add JSR223 PostProcessor as a child of the getForm request
  2. Choose groovy in the "Language" drop-down
  3. Put the following code into "Script" area:

    import groovy.json.JsonBuilder
    import groovy.json.JsonSlurper
    
    def response = prev.getResponseDataAsString()
    def json = new JsonSlurper().parseText(response)
    
    def builder = new JsonBuilder(json)
    builder.content.property2 = '2.1'
    
    vars.put("response", builder.toPrettyString())
    
  4. In saveForm request use ${response} as the request body

References:

Upvotes: 6

omalyutin
omalyutin

Reputation: 454

I have managed to do this via BeanShell PostProcessor. First I have added a new BeanShell PostProcessor: click right button on getFrom, Add -> Post Processors -> BeanShell PostProcessor. After that I opened the reated BeanSChell and have written the following code in Script field:

responseString = vars.get("response");
log.info("Received response: " + responseString);
responseString = responseString.replace("\"prop1\" : \"value2\"", "\"prop1\" : \"value2.1\"");
log.info("Response to send: " + responseString);
vars.put("modifiedResponse", responseString);

In Parameters field I have written:

response

Finally, I changed for PUT request the body from

${response}

to

${modifiedResponse}

Upvotes: 0

Related Questions