How to see the response headers in REST message?

dwilson10
Kilo Contributor

I'm currently creating REST Messages using in ServiceNow to make API calls an outside server using their API resource. I'm having issues retrieving and storing a non standard header generated by the server's API. Using the RESTLet client chrome extension, I'm able to see the token necessary in the response header. I'm unsure how to do the same in ServiceNow.

3 REPLIES 3

Brad Tilton
ServiceNow Employee
ServiceNow Employee

Have you tried using restmessagev2's getRequestHeaders() method or are you doing it another way?



RESTMessageV2 - Scoped, Global


dwilson10
Kilo Contributor

Originally no I didn't use the restmessagev2's getRequestHeaders() method. I was trying to pass the token in the HTTP Header under the HTTP requests under the get method. Even when using the restmessagev2's getRequestHeaders() method no success. I'm very new to serviceNow and have only been developing on the platform for almost two weeks. I'm using these REST messages I create to run from a workflow for new a catalog service item. Like i said using the RESTlet client from the chrome store I'm able to see exactly what I need and how to pass the token. Just unsure of how to do it in ServiceNow.





RESTlet client shot 1.jpg



This is making the first call to generate the response header necessary.



RESTlet client shot 2.jpg


The needed token is the X-RestSvcSessionId.


snehalkhare
Kilo Guru

Hi,

This is an old post but hope my reply could be helpful.

In order to get the Response headers in ServiceNow you can use the function - getAllHeaders()

Sample syntax:

var r = new sn_ws.RESTMessageV2('RESTMESSAGE NAME', 'get');

var response = r.execute();

var headers = res.getAllHeaders(); // returns all headers present in the response

for(var i in headers) 

{

gs.log(headers[i].name + ': ' + headers[i].value);

}

To get specific header  use - getHeader(String name)

Example

var sm = new sn_ws.RESTMessageV2("RESTMESSAGE NAME","get"); 
var response = sm.execute();
var headerVal = response.getHeader("Content-Type");