Setting HTTP Body during runtime

@Ibus
Any thoughts, how best I can build the binding with multiple variables ?
Appreciate your help.

@spullabhotla they way you did it should just work … i will post a bit later an example of how i did it, but is mostly the same, the only difference is I use the template stored in an external file to make the script shorter …

add a println(text) after you build the body to check if the substitution was right, perhaps your test is failing from a different cause …

@spullabhotla
So, here is a sample code i use for a given API:

WS.comment("Setup")
reqUrl = "${GlobalVariable.proto}://${GlobalVariable.Url}/${GlobalVariable.Endp}"
jsonTemplate = new File(path/to/BodyFile).text

bindings = [ 'id': CampaignId, 'sTerm': SearchTerm, 'prdId': ProductId, 'token': Token ]
engine = new groovy.text.SimpleTemplateEngine()
reqBody = engine.createTemplate(jsonTemplate).make(bindings).toString()

and the template is stored as a text file, looks like:

{
"campaignId":"${id}",
"searchTerm":"${sTerm}",
"productId":${prdId},
"token":"${token}"
}

the ‘varName’ variables in bindings = [] correspond to the ${varName} in the template.

the values passed to the bindings are variables declared in the variables tab (SearchTerm and so on)

so … comparing to your code, you can try like this (notice that I am using SimpleTemplateEngine, not the Streaming one and also I call the toString() method an the end):

jsonTemplate = """{"_id": "${id}", "to": "${to}", "launch": true}"""
bindings = [ 'id' : someVariable1, 'to' : someVariable2 ]

engine = new groovy.text.SimpleTemplateEngine()
reqBody = engine.createTemplate(jsonTemplate).make(bindings).toString()

-----
request = findTestObject(‘Object Repository/external (SendGrid)/LaunchCampaign.ContentJSON(url,body,token)’, [ (‘url’) : requestUrl, (‘body’) : reqBody , (‘token’) : adminToken ])

Thank you very much - it worked. Very trustfully, appreciate your help.