Scripted REST API - dynamic URL

Colleen
Tera Expert

Is it possible to create a scripted REST API method with a dynamic URL?

 

For example, using this URL to make the REST call:

https://<servicenowURL>/api/x_uob15_living/ticket/TCK12345/comments

 

Where TCK12345 is the reference number of an existing ticket and will change depending on the ticket being queried. 

 

I tried specifying the relative URL of the method as /${ticket_id}/comments, but it was not accepted.

2 ACCEPTED SOLUTIONS

M Iftikhar
Mega Sage

@Colleen ,

In ServiceNow, when creating a Scripted REST API, it is possible to have dynamic segments in your URL path like your example with a variable ticket ID — but it must be done using path parameters.

You should define your relative path using curly braces {} to indicate a path parameter. So for your case:

/{ticket_id}/comments

Then, inside your Scripted REST Resource, you can access the dynamic value using the request.pathParams object.

 

Thanks & Regards,  
Muhammad Iftikhar  

If my response helped, please mark it as the accepted solution so others can benefit as well. 

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Colleen 

why not?

It should work when you use GET Method.

you can grab the ticket_id in scripted rest api script

check solution shared below from me

How to use Query Parameter of Scripted API 

there are lot of OOTB Scripted REST API which you can check how to grab it

AnkurBawiskar_0-1758892968959.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

5 REPLIES 5

M Iftikhar
Mega Sage

@Colleen ,

In ServiceNow, when creating a Scripted REST API, it is possible to have dynamic segments in your URL path like your example with a variable ticket ID — but it must be done using path parameters.

You should define your relative path using curly braces {} to indicate a path parameter. So for your case:

/{ticket_id}/comments

Then, inside your Scripted REST Resource, you can access the dynamic value using the request.pathParams object.

 

Thanks & Regards,  
Muhammad Iftikhar  

If my response helped, please mark it as the accepted solution so others can benefit as well. 

Thanks Muhammed.

I was using the wrong syntax for the dynamic path parameter.

Ankur Bawiskar
Tera Patron
Tera Patron

@Colleen 

why not?

It should work when you use GET Method.

you can grab the ticket_id in scripted rest api script

check solution shared below from me

How to use Query Parameter of Scripted API 

there are lot of OOTB Scripted REST API which you can check how to grab it

AnkurBawiskar_0-1758892968959.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

MackI
Kilo Sage

hi @Colleen 

 

The issue is with the ${...} format. ServiceNow's Scripted REST API framework uses curly braces {} directly to define path parameters, not the dollar sign $ and curly brace combination used in JavaScript template literals.

So, to fix it, you just need to remove the dollar sign. The correct relative path should be:

/{ticket_id}/commentsWhen you define a resource (like a GET or POST method) for your Scripted REST API, you set its "Relative path". Instead of a static path like /incident, you can create a dynamic one like /incident/{number}.

When a request comes in to a URL like .../api/your_api/incident/INC0010001, ServiceNow automatically captures the value INC0010001 and makes it available inside your script via the request.pathParams object.

This allows a single API resource to handle a wide range of requests dynamically

 

Another important thing, you need to write a scripted REST API.

 

Disclaimer: Please note that the information and advice provided above are based on my personal experience and are not official solutions or professional advice from DXC Technology. No patent code or solution from DXC Technology has been shared

If this response helped resolve your problem, please kindly mark it as the Best Answer ‌🌠‌. If it gave you some useful insight, a Helpful ‌‌ mark would be wonderful too! Your feedback encourages me to contribute more to our amazing community.

 

MackI | ServiceNow Technical Consultant | DXC Technology Australia | ServiceNow Practice | LinkedIn Top IT Operation Voice 2023 | Sydney,Australia