Designing Pages Properly: Path Variables and Query Parameters

As a web frontend developer, have you ever wondered whether to use a path variable or a query parameter?

I have too. And I remember ultimately just going with whatever felt right, without any particular criteria… Reflecting on that past version of myself, let’s take a closer look.

TL;DR

Path variables and query parameters are two ways to pass data in a URL. We should choose the right one for its purpose.

Use query parameters to pass optional information for filtering, sorting, pagination, and search.

Use path variables to uniquely identify a resource or represent a unique ID.

Query Parameter

A query parameter is a variable added to the end of a URL. It follows the URL path, starts with ?, and is passed as a key=value pair. Multiple parameters are separated with &. For example, in https://example.com/search?query=react&page=2, query and page are query parameters.

/users?id=123
  • Query parameters are mainly used to pass optional data.
  • They pass options such as filtering, sorting, and pagination.
  • Since they appear explicitly in the URL, they can easily express complex search or filtering conditions.
  • Search engines can understand and crawl query parameters, but their meaning may be weaker for SEO than path variables.

Path Variable

A variable included in the URL path.

Path variables mainly represent a resource’s unique identifier, such as a user ID or product ID.

/users/123
  • Since it is part of the path itself, use it for required values.
  • It is common in RESTful APIs. For example, GET /users/123 requests information about user 123.
  • For search engine optimization (SEO), URLs containing path variables can be more intuitive and meaningful, which can help SEO.
  • Path variables are useful for representing a resource’s hierarchical structure. For example, /users/123/posts/456 points to post 456 belonging to user 123.

So which one should we use?

Comparison of path variables and query parameters

  • Use Path Variables to uniquely identify resources, such as the unique ID of a particular user, post, or product.
  • Use Query Parameters to pass optional information such as filtering, sorting, or pagination. Their main purpose is to set additional conditions on data provided by the server.

Bonus: Array and Map Parameters

There are several ways to represent an array.

  1. Bracket notation

We’ve recently been implementing array parameters in this form. The key ends in [], and repeated keys are added with the & separator.

When someone asked why we’d chosen this approach, I couldn’t answer.

Even though we were using it correctly… :(

/authors?name[]=colleen&name[]=hoover
  • Including [] in the key makes the array form explicit. This approach is commonly used by server frameworks.
  1. Repeating the parameter name
/authors?name=colleen&name=hoover
  • This is one of the standard approaches under RFC 3986, allowing the same parameter name to appear multiple times. It is often used when one parameter corresponds to multiple values.
  • A server that handles single values rather than arrays might use only the last value or overwrite duplicates, so developers may need to handle this explicitly.
  1. Comma-separated values
/authors?name=colleen,hoover
  • Separating multiple values with commas keeps URLs clean and makes array semantics easy to convey. It can be more intuitive.
  • Some server implementations may not support it as a standard format, requiring additional processing so the server can interpret it.
  1. URL encoding
/authors?name=%5Bcolleen%2Choover%5D
  • URL encoding allows arrays to be passed safely. Characters are encoded for transmission, making it suitable for data containing special characters.
  • It is less readable and can make debugging difficult. The server also needs to interpret the encoded data correctly, which may require additional logic.

Of these, repeating the parameter name is the most standard approach. Most servers and clients support it well, and it is closer to web standards. In certain frameworks, bracket notation can be useful for representing arrays.


References

https://medium.com/@fullsour/when-should-you-use-path-variable-and-query-parameter-a346790e8a6d

https://dev.to/farhatsharifh/path-vs-query-parameters-choosing-the-right-approach-for-api-requests-2lah

https://www.atatus.com/blog/rest-api-best-practices-for-parameter-and-query-string-usage/