Middle
What is the difference between path parameters and query parameters?
sobes.tech AI
Answer from AI
Path parameter and query parameter are two ways of passing data in a URL, but they differ in purpose and syntax.
-
Path parameter is part of the URL path, used to identify a specific resource. For example, in the URL
/users/123, the number123is a path parameter representing the user ID. -
Query parameter are parameters passed after the
?sign in the URL, usually for filtering, sorting, or passing additional options. For example,/users?sort=asc&limit=10— here,sortandlimitare query parameters.
Example:
GET /products/45 # path parameter: 45 — product ID
GET /products?category=books&price_lt=100 # query parameters: filters
Thus, path parameters are usually mandatory and define the resource, while query parameters are optional and refine the request.