How to get a text/html response in a scripted rest resource

JC59
Giga Contributor

I'm using a scripted rest api to collect a response from a user through an email. The rest url is embedded in an anchor tag inside an email template like the following:

<p><a href="https://myinstance.service-now.com/api/x_ntc_myapp/myscript?mcid=${sys_id}&amp;res=Yes">YES</a></p>
<p><a href="https://myinstance.service-now.com/api/x_ntc_myapp/myscript?mcid=${sys_id}&amp;res=No">NO</a></p>

I want to return a response to the user in HTML saying something like "Thank you for your response".

The script works correctly but I am unable to return a "text/html" response. I have tried setting the content-type in the script similar to:

var body = "<html><head><body><p>Thank You for your response.</p></body></head></html>";
var hdrs = {};
hdrs['Content-Type'] = "text/html";
response.setHeaders(hdrs);
//response.setContentType("text/html");
response.setBody(body);
response.setStatus(200);

But I get the following error response back :

<?xml version="1.0" encoding="ISO-8859-1"?>

<response><error><detail>Cannot convert <html><head><body><p>Thank You for your response.</p></body></head></html> to org.mozilla.javascript.ScriptableObject (sys_ws_operation.280cdc4c1b0c0c10c34b419ead4bcbda.operation_script; line 27)</detail><message>Script Evaluation Exception</message></error><status>failure</status></response>

How do I set the headers to get an HTML response? I understand this is not a typical scripted rest scenario since I am calling it from an email but displaying the response in the browser as xml is not very user friendly.

If I use the default content types and set the response like this:

var body = {};
body.text = "Thank You for your response.";
response.setBody(body);
response.setStatus(200);

Then I can get back an XML response in the browser.

<?xml version="1.0" encoding="ISO-8859-1"?>

<response><result><text>Thank You for your response.</text></result></response>

 

 

1 ACCEPTED SOLUTION

JC59
Giga Contributor

That was helpful but could not get XMlHelper to work in my scoped application. But after looking at your response I realized I just needed the stream writer to return html.

 

response.setContentType("text/html");

response.setStatus(200);
var writer = response.getStreamWriter();
writer.writeString("<html><head><body><p><b>Thank You for your response.</b></p></body></head></html>");

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the content type should be application/xml while returning the response

you need to create json object first with the key:value pair

then use XMLHelper class to convert it to xml and use writer

updated script here; try and check one

var body = {};
body.text = "Thank You for your response.";
response.setContentType("application/xml");

response.setStatus(200);
var writer = response.getStreamWriter();
var xhelper = new XMLHelper();
writer.writeString(xhelper.toXMLStr(body));

screenshot from my scripted rest api after testing from Postman

find_real_file.png

 

Screenshot when tested in browser after hitting the endpoint url

find_real_file.png

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

JC59
Giga Contributor

That was helpful but could not get XMlHelper to work in my scoped application. But after looking at your response I realized I just needed the stream writer to return html.

 

response.setContentType("text/html");

response.setStatus(200);
var writer = response.getStreamWriter();
writer.writeString("<html><head><body><p><b>Thank You for your response.</b></p></body></head></html>");

Hi Jc,

Did you accidentally mark your own answer as correct instead of mine?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader