We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Looking for examples or scenarios where pathparams and queryparams can be used in scripted rest api

Suggy
Giga Sage

Hello....

 

I know how pathparams and queryparams work in scritped rest api. but I feel both are the same or dedundant. ie I think both can be used interchangeable.

 

Can anyone give examples or scenarios where pathparams and queryparams can be used in scripted rest api where each has got its own significance?

4 REPLIES 4

Suggy
Giga Sage

ANYONE?

Rajdeep Ganguly
Mega Guru

Sure, both path parameters and query parameters are used in Scripted REST APIs in ServiceNow, but they serve different purposes and are used in different scenarios. Here's a summary: Path Parameters: - Path parameters are part of the URL path. They are used to identify a specific resource or resources. - They are useful when you want to get a specific record or set of records. For example, if you have a REST API for users, you might have a path like "/users/{userID}" where userID is a path parameter. - Path parameters are mandatory. The API will not work if they are not provided. Query Parameters: - Query parameters are appended to the URL after a '?' symbol. They are used to filter or sort the data that is returned by the API. - They are useful when you want to limit the data that is returned. For example, you might have a query parameter like "?status=active" to only return active users. - Query parameters are optional. The API will still work if they are not provided, but it might return more data than you need. Here's an example of how you might use both in a Scripted REST API in ServiceNow: 1. Create a Scripted REST API with a resource path like "/users/{userID}". 2. In your script, use request.pathParams.userID to get the userID from the path parameter. 3. Use this userID to get the specific user record from the database. 4. Also check if there is a query parameter for the status. You can do this with request.queryParams.status. 5. If the status query parameter is provided, use it to filter the user records that are returned. 6. Return the filtered user records in the response. This way, you can use the path parameter to specify which user records you want, and the query parameter to further filter these records.

Suggy
Giga Sage

.

Anand Kumar P
Tera Patron

Hi @Suggy,

In ServiceNow Scripted REST APIs, path parameters and query parameters serve distinct purposes and are used in different scenarios.

1. Path Parameters:
 Purpose: Path parameters are part of the URL path and are used to identify a specific resource.
Example:

/api/v1/incident/{incident_id}

Scenario:You want to retrieve details of a specific incident identified by its unique ID. The incident_id is embedded in the URL path.

2. Query Parameters:
• Purpose: Query parameters are part of the URL query string and are used for filtering or modifying the results.
Example:

/api/v1/incidents?status=open&priority=high

 Scenario:You want to retrieve a list of incidents with specific criteria, like those with an open status and high priority. The parameters are appended to the URL as key-value pairs.

Real-time Example:
Suppose you have a Scripted REST API to manage tasks. Here’s how you might use both path parameters and query parameters:

• Path Parameters:
• GET /api/v1/task/{task_id}
• Purpose: Retrieve details of a specific task identified by its task_id.
• Query Parameters:
• GET /api/v1/tasks?status=incomplete&assigned_to=JohnDoe
• Purpose: Retrieve a list of tasks filtered by status and assigned_to.

In this example, path parameters help you specify a particular task, while query parameters allow you to filter the list of tasks based on various criteria.

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand