How to dynamically set the Authenticate key in the http header of rest Message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2020 03:39 AM
Hello all,
We are implementing an integration using REST Api ,and in that we are having the same end point but different Authentication Keys for different region,so how can we set them dynamically so that it load all the data from 2 regions.
Currently in one method we are generating organization there i have tried to use both the value but its giving me the organization whose key i have mentioned in the Parent Rest Message.
SO how can i achieve this ?
Help from anyone would be appreciated.
Regards,
Riya
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2020 06:46 AM
Hi Riya,
Please try testing this web service from REST Message first and check if you are able to connect to web service and getting the desired output.
In order to set the values dynamically, you can use use below methods:
sm.setRequestHeader("Accept","Application/json"); // to set header information
sm.setStringParameterNoEscape("s","NOW"); // to pass any variable and it's value
In order to pass the ID as part of second request. You need to get the ID from your first request in some variable and pass it as parameter in your second call.
Hope that helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2020 07:17 AM
Hello Nikhil,
Sorry if i am asking silly questions as i am fairly new to this.
I have tested the Rest Message by manually adding the token value in Http Header for each organization separately.
Its getting connected and giving the success.
But now as you told to include this in script in order to pass dynamic value I have added that:
Let me show you:
var del = new GlideRecord("Staging table");
del.query();
del.deleteMultiple();
//Getting Organizations
var org = new sn_ws.RESTMessageV2('Rest message name', 'Organizations');
org.setRequestHeader('Accept','Application/json');
org.setStringParameterNoEscape('token','Value1');
org.setStringParameterNoEscape('token','Value2');
var res = org.execute();
var resBody = JSON.parse(res.getBody());
var httpStatus = res.getStatusCode();
if(httpStatus == 200){
for(var j=0;j<resBody.length;j++){
//Getting All Devices in each and every Organization
var rmDev = new sn_ws.RESTMessageV2('Rest Message Name', 'Method to get data from organization');
rmDev.setStringParameterNoEscape("orgId",resBody[j].id+"");
Can you please check if this is right or not?
And also while I am testing the organization Method again after including values here its getting failed.
Need to know do i have to include ${token} in endpoint also of the HTTP Method?
And after adding header here I have removed that from the Rest message.Is this right?
2.For the 2nd point we are parsing the response of 1st method to get the ID.Do I need to change something here as now we will have multiple IDS for different organizations.
Many Thanks,
Riya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2020 09:57 PM
Hi Riya,
Above script looks ok to me and should work. Some obeservations here as below.
org.setStringParameterNoEscape('token','Value1');
org.setStringParameterNoEscape('token','Value2');
I hope you are not using both the tokens in single request. If you want to get data from org1 then you should pass the token for org1 only.
Additionally, as you mentioned you were able to get correct response by executing request from REST Message, you can use "Preview Script Usage" functionality under related links to generate sample code. You can compare that code with above one and check what corrections are required.
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2020 10:12 AM
Hi Riya,
So what I could get from your points is how to set the token in the endpoint?
Did you create variable substitutions for this token variable?
if you don't require to create variable substitution then you can set the header using setRequestHeader()
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.
Regards
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
06-23-2020 04:15 AM
Hello Riya,
In addition to nikhil's answer I would like to add one approach to achieve this. you can create to methods for GET and add similar details(endpoint) with different authentication key and header.
Hope this make scene.
Regards,
Akshata