Path Parameter Vs Query Parameter

NehaSNOW
Tera Guru

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,

9 REPLIES 9

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.

Riya Verma
Kilo Sage
Kilo Sage

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.

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Good explanation.

 

But how to decide which one to use when?

@NehaSNOW ,

 

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:

  1. 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.

  2. 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.

  3. 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.

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

@NehaSNOW , if its helpful and worked for you , can you please accept the sollution so that thread is closed. 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma