Query string

A query string is the part of a URL that includes one or more query parameters, giving more details about the request.

It starts after a ? and can include multiple parameters separated by &, helping to refine the data you want to receive.

They can be used on websites, or when making an API request.

Example of a API request

Let’s say you want to get the current weather for the city Amsterdam, and you want the degrees in metric instead of imperial.

To let the API know which city, we have to add it as a query string to our request:

https://api.weather.com/v3/weather/current?city=Amsterdam&units=metric

So here, the query string has two query parameters:

  • city=Amsterdam specifies the city
  • units=Metric sets the system of measurement

And the query string is put together by starting with a ? after the base URL and then separating each query parameter with &.

Example on a website

Imagine you’re on an online bookstore. If you want to search for science fiction books, you might enter your query in the search bar. The website might then generate a URL like this:

https://www.bookstore.com/search?genre=science-fiction&sort=price

In this URL:

  • genre=science-fiction is a query parameter specifying that you want books in the science fiction genre.
  • sort=price is another query parameter that instructs the website to sort the results by price.

Interesting, right? πŸ˜„