Forum Discussion

travisSAEG's avatar
travisSAEG
New Contributor
10 months ago

need to append json to Request as it is built using Groovy step

I am building JSON using a groovy step that will be different for each iteration of my testcase.  I found a code snippet (see below) that will set the request content but that command will replace the request information rather than just append to it.  What would I use as the groovy command to just append to the request? Also my JSON is very large (over 240,000 bytes).  The command below that is bolded replaces the entire JSON in the request .  What I need is the command that would just append the JSON lines I just built.  

 

Here is something I tried to put several pieces of JSON together from small hardcoded JSON snippets using the bolded command (it worked).

request.setRequestContent(JsonOutput.prettyPrint(temp)+ temp2 + JsonOutput.prettyPrint(temp3) + tempEnd)

 

CODE SNIPPET

def stepName='restStep'
def request = context.testCase.getTestStepByName(stepName).getTestRequest()
def jsonText = '''
{
"id" : "sample id",
"name" : "sample name",
"tags" : [ "sample tags" ],
"address" : {
"street" : "sample street",
"zipcode" : "sample zipcode",
"city" : "sample city"
}
}
'''
request.setRequestContent(jsonText)

5 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    Possible to provide the use case? You may use sample data if needed to explain things for better understanding.

  • I have attached the initial schema of the JSON I am working with.  It has several pieces to it (Forms, Fields).  Based on the different Types of data I am dealing with I use a variety of data from various sources (xlsx, randomization, hardcoded). to fill in the values and data fields to build the final request body that goes to the API.  The schema can change some based on what state we are dealing with as well.  After the initial schema is created my code will parse it and fill in the data fields based on the types for the different parts of the JSON schema.  I have been able to use the above code snippet to build the final JSON at the end of the code after I have built all the separate JSON pieces (that works).  What I was hoping for was something to replace the one time build of the JSON (see bolded code in my initial post) with one that does an append instead for each JSON piece.  Just curious if such a code command exists that does an append.  

  • nmrao's avatar
    nmrao
    Champion Level 3

    How do you get the extra content for each iteration which needs to be appended to existing json content?

    if you give couple of examples of that extra piece of json needs to be appended and where in the json and expected output, that will help!

    • travisSAEG's avatar
      travisSAEG
      New Contributor

      Thanks for the replies.  I have at least 2 main pieces I have to build separately and that is the Fields portion and the Forms portion to get the JSON completed.  these two pieces needed to be appended to each other in order to build the main part of the JSON.  The command I stated in my first posting of request.setRequestContent(jsonText) will only replace (overwrite) the JSON (that is why I concatenate the fields and forms pieces inside the () of the command replacing jsonText).  All I want to know is there another code command that will append to instead of replacing/overwriting what I already may have in my JSON.  I have this command working fine and building all of my JSON correctly as I am using it.

  • nmrao's avatar
    nmrao
    Champion Level 3

    Ok.

     

    The original json content from the request needs to be saved into a temporary file, say a template, as this content is dynamic to each test.

    Read the json from that template each time and append to it and then set to the request..