- Technicalpig
- Posts
- TechnicalPig🐷: "Query Params" vs "Path Params"
TechnicalPig🐷: "Query Params" vs "Path Params"
When to use them in API development
The terms "query params" and "path params" often come up in the context of web development, specifically when dealing with HTTP requests. While they are both used to pass information between the client and server, they serve different purposes and are used in different parts of the HTTP request. Here's a breakdown of their differences:
Query Parameters (Query Strings)
Location: Encoded in the URL after the question mark (
?
).Usage: Primarily used in
GET
requests to filter resources or specify certain data that is being requested from the server. For example, in a request to a search engine, query parameters might include search terms, filters like date ranges, or sorting preferences.Visibility: Visible in the URL, which means they can be bookmarked or shared. However, this also means they should not be used for sensitive information.
Example:
https://example.com/api/posts?search=api&sort=asc
Path Parameters (Route Params)
Location: Included in the URL's path itself.
Usage: Used to identify a specific resource or a group of resources. They are often used in
GET
,PUT
,DELETE
, andPOST
requests to specify the identity of the resource(s) upon which the server should act.Visibility: Since they are part of the URL's path, they are also visible. However, they are typically used for necessary identifiers rather than optional data.
Example:
https://example.com/api/posts/123
where123
is a path parameter specifying a particular post.
When to use Query Params vs. Path Params:
Query Parameters: When you need to filter or sort resources, or pass optional data to the server that doesn't identify a resource by itself. They are more flexible for searches, filtering, and pagination.
Path Parameters: When you're identifying specific resources or a group of resources. They are part of the URL's structure and indicate a hierarchical relationship between resources.
Summary
In summary, the choice between query parameters and path parameters depends on what you are trying to achieve with your HTTP request. Path parameters are for specifying the resource you are interested in, while query parameters are for sorting, filtering, or detailing the request further.
Questions
Should userID be in the query param or path param?
Should a search term like “chicken” be passed as a query param or path param?
Answers:
For direct, specific operations on a user's account or data (like retrieval, update, or deletion), the user ID should be passed as a path parameter. This approach is more semantic and aligns with RESTful API design, providing clear, hierarchical URLs that represent the structure of the application's data.
A search term like "chicken" should be passed as a query parameter. This is because "chicken" is being used to filter or search through resources, rather than identifying a specific resource by itself. Query parameters are ideal for this purpose because they allow for a dynamic set of criteria for filtering results without changing the structure of the API's endpoints.