Newline characters added to a string - stopping the integration with 400 bad request

nirwan_ritik
Tera Contributor

Hi All,

We have an integration trigger set via a flow designer. It has a script step which builds a string variables named 'details'. It contains few ticket details separated by newline:

syntax:
var details = "";

details += "Short description: " + inputs.short_desc + " ";

details += "\n Description: " + inputs.desc + " ";

details += "\nRequested for: " + inputs.requested_for + " ";

and this details variable is then added in the REST payload.

But it's failing! Anyone faced similar issue?

Regards

2 REPLIES 2

mayankkumar
Kilo Patron

The issue is most likely due to the \n newline characters being passed directly into the REST JSON payload.

You can fix this by properly escaping the string before sending it, for example:

var details = "";

details += "Short description: " + inputs.short_desc + "\n";
details += "Description: " + inputs.desc + "\n";
details += "Requested for: " + inputs.requested_for;

details = JSON.stringify(details);
details = details.substring(1, details.length - 1);

Alternatively, use GlideStringUtil.escapeNonPrintable() or, preferably, the JSON Builder in Flow Designer to construct the payload. This ensures newlines and other special characters correctly escaped.

 -----------------------------------------------------------------------------------------------------------------------------------------
Please mark this response helpful and accept as solution
Thanks & Regards
Mayank

prerna_sh
Kilo Patron

Hi @nirwan_ritik 


Please check out the similar thread:
https://www.servicenow.com/community/developer-forum/flow-designer-rest-step-json-parsing-error-due-... 

-------------------------------------------------------------------------------------------------------------------------------------------
If my response solves your query, please mark helpful by selecting accept as Solution and Helpful. Let me know if anything else is required.
Thanks,
Prerna