我正在使用 Katalon Studio 发送 HTTP 请求。在我发送请求之前,我想编辑 JSON 对象的主体。
JSON - 正文
{
"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"
}
发送请求而不编辑正文时:打印正确的 JSON
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"
}
当我编辑 http 正文并尝试使用生成的添加变量添加我自己的转义字符串时,我得到以下输出:
//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)
输出:
"{
"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"
}"
编辑正文后,JSON 在开头和结尾处都有逗号。不会打印随机字符串值,而是打印名称。
在字符串中使用 JSON 时,任何人都可以帮忙转义字符吗?