Path Parameter Vs Query Parameter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 12:24 AM - edited 06-23-2023 12:53 AM
Hi Team,
Would like to know the difference between Path Parameter and Query Parameter with an example.
I saw many answers in the Google but here I am looking about your understanding. Please share your own experience.
Also, would like to know how to decide which of them to use when?
Please suggest.
Thanks,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 01:08 AM
Yes you can, but it is not considered good practice. It's like a lot of thing in ServiceNow, you can accomplish things in a lot of ways, but not all ways should be used.
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 02:17 AM
Hi @NehaSNOW ,
Hope you are doing great.
Path Parameters: Path Parameters are used to include dynamic values directly in the URL path of a request. They are denoted by a specific placeholder within curly braces {} in the URL.
Endpoint: /api/users/{userId}
In this case, {userId} is a path parameter, and the actual user ID value would be provided in its place in the URL. For instance, if the user ID is "12345", the complete URL would be /api/users/12345.
Query Parameters are appended to the URL as key-value pairs, separated by an ampersand (&). They are used to provide additional information or parameters to the server. Query parameters do not impact the structure of the URL itself, and they can be optional.
Endpoint: /api/users?role=admin&active=true
In this case, the query parameters "role" and "active" are appended to the URL. The server can then use these parameters to filter and return the desired user list based on the criteria specified.
path parameters are used when you need to identify or retrieve a specific resource directly from the URL path, while query parameters are used for providing additional information or parameters to the server.
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 02:38 AM
Good explanation.
But how to decide which one to use when?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-23-2023 02:55 AM
Decision on whether to use query parameters or path parameters depends on the context and purpose of the data you are working with. Here are some factors to consider:
If you need to uniquely identify a specific resource in the URL, path parameters are more appropriate. For instance, if you want to retrieve a particular user's details, using a path parameter like "/api/users/{userId}" makes sense.
Filtering and sorting: If you need to filter or sort data based on different criteria, query parameters are more suitable. They allow for flexible filtering options by appending multiple parameters to the URL.
Readability and maintainability: Consider the readability and maintainability of your API endpoints. Path parameters can make the URL structure more explicit and descriptive, while query parameters can make the URL more cluttered and less readable, especially when multiple parameters are involved.
Regards,
Riya Verma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 02:36 AM
@NehaSNOW , if its helpful and worked for you , can you please accept the sollution so that thread is closed.
Regards,
Riya Verma