Forum Discussion

jmahant's avatar
jmahant
Occasional Contributor
7 years ago

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.

  • 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.
  • Radford's avatar
    Radford
    Super Contributor

    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.
    • jmahant's avatar
      jmahant
      Occasional Contributor

      Aplogies for delay in responding but your suggestion did help.

      Thanks :)