cancel
Showing results for 
Search instead for 
Did you mean: 

Customize the number of key value pairs in a request based on parameter value.

SOLVED
Highlighted
Occasional Contributor

Customize the number of key value pairs in a request based on parameter value.

I have a patch request which can be used for multiple categories (as parameter) as following

PATCH {URL}/categories/<CategoryName>/values 
[{
"name": "<Para1_Name>",
"value": "<Para1_value>"
},
{
"name": "<Para2_Name>",
"value": "<Para2_value>"
}]

 

We have multiple categories and the only thing that varies for these categories is the "number" of input parameters, that they support as part of above call.

What I want to accomplish is to reuse the same call but dynalically update the number of input parameters based on the category name.

 

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Super Contributor

Re: Customize the number of key value pairs in a request based on parameter value.

I've done this but in my case it was name/value pairs in XML for a SOAP service, from your data I assume you have JSON data for a REST request.

 

This is just a high level overview of what I would do, all within a groovy test step before your requst test step:

 

  1. Get you JSON data from your REST request, easiest way via the GetData functionality.
  2. Use the JsonSlurper to convert your data to a Groovy object.
  3. Manipulate you data to add and remove the name value pairs via GPath expressions.
  4. Use the JsonOutput to convert you Groovy JSON objects back to JSON data string.
  5. Update your REST request with your new data.
2 REPLIES 2
Super Contributor

Re: Customize the number of key value pairs in a request based on parameter value.

I've done this but in my case it was name/value pairs in XML for a SOAP service, from your data I assume you have JSON data for a REST request.

 

This is just a high level overview of what I would do, all within a groovy test step before your requst test step:

 

  1. Get you JSON data from your REST request, easiest way via the GetData functionality.
  2. Use the JsonSlurper to convert your data to a Groovy object.
  3. Manipulate you data to add and remove the name value pairs via GPath expressions.
  4. Use the JsonOutput to convert you Groovy JSON objects back to JSON data string.
  5. Update your REST request with your new data.
Occasional Contributor

Re: Customize the number of key value pairs in a request based on parameter value.

Aplogies for delay in responding but your suggestion did help.

Thanks Smiley Happy

New Here?
Join us and watch the welcome video:
6 / 7 API Testing Mistakes Video
Top Kudoed Authors