Query Parameters vs Path Parameters
π‘ Concept Name
Query vs Path Parameters β These are parts of a URL in REST APIs used differently: path parameters pinpoint specific resources, while query parameters help filter, sort, or paginate data.
π Quick Intro
Path parameters are embedded directly in the URL path (e.g., /users/42
), uniquely identifying resources. Query parameters come after a ?
and provide optional instructions to modify the returned data, like ?limit=10&sort=name
.
π§ Analogy / Short Story
Think of a hotel stay: the room number is like a path parameter, identifying your exact room. Special requests such as βextra towelsβ or βlate checkoutβ act like query parameters, customizing your experience without changing the room itself.
π§ Technical Explanation
- π§ Path Parameter: Part of the URL path that specifies the resource ID or name, e.g.,
/products/25
. - π Query Parameter: Appended to the URL after
?
, used to filter, sort, or paginate data, e.g.,?page=2&sort=desc
. - π Best Practice: Use path parameters for mandatory resource identification and query parameters for optional data modifications.
- π Combination: Commonly combined in RESTful APIs, e.g.,
/users/10/posts?limit=5
.
π― Purpose & Use Case
- β
Path Parameters: Used to identify a specific resource, e.g.,
/orders/123
fetches order 123. - β
Query Parameters: Filter or modify data results, e.g.,
/orders?status=shipped&limit=10
filters shipped orders. - β Widely used in search, filtering, sorting, and pagination in REST APIs.
π» Real Code Example
// Fetch user with path parameter
fetch('/api/users/42');
// Search users with query parameters and pagination
fetch('/api/users?search=admin&page=2&limit=10');
// Combine path and query: Get posts by user with pagination
fetch('/api/users/42/posts?limit=5&sort=desc');

β Interview Q&A
Q: What is the main difference between query and path parameters?
A: Path parameters identify a specific resource, while query parameters filter or modify the request.
Q: Where do query parameters appear in a URL?
A: They appear after the ?
symbol as key=value pairs.
Q: Are path parameters mandatory?
A: Usually, yes. They are required to locate a specific resource.
Q: Can query and path parameters be used together?
A: Absolutely. Itβs common to combine them for more flexible API queries.
Q: Which parameter type is best for filtering results?
A: Query parameters are ideal for filtering, sorting, and pagination.
π MCQs
Q1. What is a path parameter?
- Appends data to response
- Used to filter results
- Identifies a specific resource in the URL
- Defines a query string
Q2. Which of the following is a query parameter?
- /users/5
- ?sort=name
- /products/123
- None
Q3. Which parameter type is optional?
- Path parameter
- Query parameter
- Both required
- None are optional
Q4. Where do query parameters appear?
- Inside headers
- After a slash (/)
- In cookies
- After a question mark (?)
Q5. Which is true about path parameters?
- Used for pagination
- They identify a single resource
- Optional by default
- Used for caching
Q6. Which format combines both parameters?
- ?page=2
- /users/10
- /posts
- /users/10/posts?limit=5
Q7. Are query parameters case-sensitive?
- No
- Yes
- Only on POST
- Depends on the browser
Q8. What is the correct way to pass multiple query parameters?
- ?page:1/limit:10
- ?page=1&limit=10
- ?page=1;limit=10
- /page=1/limit=10
Q9. What should path parameters never include?
- Numbers
- Slashes
- Special characters like ?
- Alphabets
Q10. Which parameter would you use to sort results?
- Path parameter
- Query parameter
- None
- Header
π‘ Bonus Insight
Follow REST guidelines by using path parameters to specify the resource and query parameters to control the output, such as filtering or sorting. This improves API clarity, caching efficiency, and SEO friendliness.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!