How do I get a new line to not give an error in an outbound REST message

Wayne Richmond
Tera Guru

I have a outbound REST integration with a supplier. I am trying to pass Work Notes via the REST message, however, if there is a new line in the note, I get the following error in my logs:

{"error":{"message":"Exception while reading request","detail":"The payload is not valid JSON."},"status":"failure"}

 

I have tried replacing \n with \\n so it includes the \n in the payload, however, it doesn't seem to process this correctly. When I view my logs, it appears okay in the list view, but doesn't look right when I open the log record.

 

List view:

listview.png

 

Record view:

logview.png 

 

Here is my script that forms the request body:

var wnString = current.work_notes.toString().replace(/\n/g, '\\n');
    gs.info('XXXXX Problem Integration wnString: ' + wnString);

    var request = new sn_ws.RESTMessageV2('XX Problem', 'Update Problem');
    request.setStringParameterNoEscape('u_customer_reference', current.number);
    request.setStringParameterNoEscape('description', descString);
    request.setStringParameterNoEscape('short_description', current.short_description);
    request.setStringParameterNoEscape('priority', current.priority);
    request.setStringParameterNoEscape('state', current.state);
    request.setStringParameterNoEscape('rootcause', current.u_root_cause);
    request.setStringParameterNoEscape('workaround', current.work_around);
    request.setStringParameterNoEscape('comments', wnString);

 

How can I send Work Notes and other string fields that include new lines via the JSON request?

1 ACCEPTED SOLUTION

Absolutely! You can even use setRequestBody() while still using the "Update Problem" method. To do that all you need to do is clear the Content field on the record and then build your payload object in your script and pass it in as a JSON string. For example:

 

var payload = {
    u_customer_reference: customerVar, // etc.
}

var request = new sn_ws.RESTMessageV2('XX Problem', 'Update Problem');
request.setRequestBody(JSON.stringify(payload));
// etc.

 

If you're unfamiliar with the API Docs site I highly suggest checking it out. Here is a link to the docs for the RESTMessageV2 API that I think you'll find very helpful while working on this integration. I hope that helps!

View solution in original post

7 REPLIES 7

Absolutely! You can even use setRequestBody() while still using the "Update Problem" method. To do that all you need to do is clear the Content field on the record and then build your payload object in your script and pass it in as a JSON string. For example:

 

var payload = {
    u_customer_reference: customerVar, // etc.
}

var request = new sn_ws.RESTMessageV2('XX Problem', 'Update Problem');
request.setRequestBody(JSON.stringify(payload));
// etc.

 

If you're unfamiliar with the API Docs site I highly suggest checking it out. Here is a link to the docs for the RESTMessageV2 API that I think you'll find very helpful while working on this integration. I hope that helps!

Thanks Dan, this is essentially how I got it working. JSON.stringify clearly takes care of all the formatting issue.

Wayne Richmond
Tera Guru

I got this working by using base64 encoding. Obviously it will need to be decoded the other end but it no longer throws an error.

var wn_base64encoded = GlideStringUtil.base64Encode(current.work_notes)