- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2019 12:02 PM
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}&res=Yes">YES</a></p>
<p><a href="https://myinstance.service-now.com/api/x_ntc_myapp/myscript?mcid=${sys_id}&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.
<response><result><text>Thank You for your response.</text></result></response>
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2019 07:15 AM
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>");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2019 09:55 PM
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
Screenshot when tested in browser after hitting the endpoint url
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2019 07:15 AM
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>");
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2019 07:50 AM
Hi Jc,
Did you accidentally mark your own answer as correct instead of mine?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader