Backslashes and underscores in REST POST API request - setBodyContent

Hello,

I need to test an API by creating a JSON to send as the body. When I create a JSON string to be inserted in the Request through setBodycontent, everything looks ok:

def deleteme = "asd"
def newJson = "{"name":"${deleteme}","surname":"${deleteme}","remember_me":0}";
println(newJson); 
// {"name":"${asd}","surname":"asd","remember_me":0} <--- OK
def newJson = '{"name":"${deleteme}","surname":"${deleteme}","remember_me":0}';
println(newJson); 
//{"name":"${deleteme}","surname":"${deleteme}","remember_me":0} <--- OK

def createPersonRequest = (RequestObject)findTestObject('CreatePersonPost')
createPersonRequest.setHttpHeaderProperties(authHeaders)
createPersonRequest.setBodyContent(newHttpTextBodyContent(newPersonJson,"UTF-8", "application/json"))
def createPersonJSON = WS.sendRequest(createPersonRequest)
println(createPersonJSON.responseText);

But the endpoint keeps giving me a request error. Looking at the server log, the endpoint receives the body message with underscores in spite of spaces and backslashes before (some) quotes:

//{"{\"name\":_\"asd\",_\"surname\":_\"asd\",__\"remember_me\":_0}": null }
//{"{\"name\":\"${_______deleteme}\",\"surname\":\"${deleteme}\",\"remember_me\":0}": null}

What am I doing wrong?
Thanks in advance

1 Like

Edit: it’s not because of this, but it seem to appear only when I set the header before with

personRequest.setHttpHeaderProperties(authHeaders);

authHeaders being the Headers array. any help?

Thanks.


Found why, surname and name were taken from an Excel file where some columns weren’t set to Text format. transforming those values in String or changing the format directly into the excel made it work.

Solved. For some reason, .setBodyContent() transformed the content-type header to a form.

header.setName("content-type")
header.setValue("application/json")

solved the issue.