I am using Katalon studio to send an HTTP request. Before I send the request I would like to edit the body of the JSON object.
JSON - Body
{
"properties":{},
"routing_key":"actions.process.x.1",
"payload": "{
\"type\":\"SEND_TWEET\",
\"twitterAccessTokens\":{
\"token\":\"abzzzzzzzzzUS38IHg3wvT7fhd63hdh3y4hfhfjr3433rcI\",
\"secret\":\"abzzzzzzzzzUS38IHg3wvT7fhd63hdh3y4hfhfjr3433rcI\"
},
\"screenName\":\"D123\",
\"text\":\" #automation_br test test test 111\"
}",
"payload_encoding":"string"
}
When sending the request without editing the body: The correct JSON is printed
def originalBody = request.getHttpBody()
System.out.println('O_Body: ' + originalBody)
Response: Start action : Statement - out.println("O_Body: " + originalBody)
{
"properties":{},
"routing_key":"actions.process.x.1",
"payload": "{
\"type\":\"SEND_TWEET\",
\"twitterAccessTokens\":{
\"token\":\"abzzzzzzzzzUS38IHg3wvT7fhd63hdh3y4hfhfjr3433rcI\",
\"secret\":\"abzzzzzzzzzUS38IHg3wvT7fhd63hdh3y4hfhfjr3433rcI\"
},
\"screenName\":\"D123\",
\"text\":\"hello test test test 1 2 3\"
}",
"payload_encoding":"string"
}
When I edit the http body and try to add my own escaped string with an added variable that gets generated I get the following output:
//String
String Body = '{\n "properties":{},\n"routing_key":"actions.process.x.1",\n"payload":"{\n \\"type\\":\\"SEND_TWEET\\" ,\n \\"twitterAccessTokens\\":{\n \\"token\\":\\"845259605840183297-O0RYViNU5mCt0WutyWdo4URGyiQLMjI\\",\n \\"secret\\":\\"78Qy1FQ26YEHMpSiMEUS38IHg3wvTLdDhwdDy0kF55rcI\\" \n },\n \\"screenName\\":\\"Deane56935078\\",\n \\"text\\":\\"@Deane56935078 #automation_br ' + randomString + ' \n}",\n "payload_encoding":"string" \n}'
System.out.println('Body: ' + Body)
//Change HTTP Body
request.setHttpBody(Body)
Output:
"{
"properties":{},
"routing_key":"actions.process.x.1",
"payload":"{
\"type\":\"SEND_TWEET\" ,
\"twitterAccessTokens\":{
\"token\":\"845259605840183297-O0RYViNU5mCt0WutyWdo4URGyiQLMjI\",
\"secret\":\"78Qy1FQ26YEHMpSiMEUS38IHg3wvTLdDhwdDy0kF55rcI\"
},
\"screenName\":\"Deane56935078\",
\"text\":\"@Deane56935078 #automation_br " + randomString + "
}",
"payload_encoding":"string"
}"
after editing the body the JSON has inverted commas at the beginning and end. The random string value does not get printed instead it prints the name.
Could anyone please help with the escape of characters when using JSONin a string?