avatarCodecat15

Summary

Query string parameters in GET API are used to filter the data requested from the server.

Abstract

Query string parameters in a GET API request are used to specify the data to be retrieved from a server. The parameters are passed to the server in the form of a string, which is separated from the base URL by the "?" symbol. Additional parameters are separated by the "&" symbol. The use of query string parameters allows for more targeted and specific data retrieval.

Opinions

  • The use of query string parameters in GET API requests is a way to customize the data retrieval process.
  • Without query string parameters, all available data would be retrieved, which could result in slower response times.
  • Query string parameters allow for more efficient and targeted data retrieval, making it a valuable tool for developers.

What are query string parameters in GET API

Have you ever seen the “?” symbol in your API request and wonder what the hell is that and what it does?

Well, many developers have had the same question but today we will explore this question with an easy example.

Let’s say you are searching a smartphone on a shopping website when you enter smartphone in the search bar you see a list of 50 smartphones to choose from

But let’s say you want a phone of a specific brand and color. In that case, you enter those filter criteria on the filter screen and the server only returns data based on your filter criteria

So in a way you are saying to the server

“Hey Mr. Server, I don’t want to see 50 smartphones but I am only interested to see the smartphones which match the criteria I entered”

If we write a function in swift for this, then it would look something like the this

But what about API’s how do we pass these parameters to an API request and let the server know what we are looking for?

This is where the “?” symbol comes into action.

The question mark symbol is the start of your query string, parameters are passed to the server with the help of query strings which help you to get specific records from the server based on the parameters you have passed

So this is how the GET API request with query string would look like

https://www.Example.com/api/smartPhone?brand=apple&color=red

Let’s understand what the above URL means

The question mark symbol “?” marks the start of our query string, where the first parameter is brand.

In brand, we are specifying that we are looking for smartphones belonging to the brand apple.

The “&” symbol denotes that there is one more parameter, which is color and here we are specifying that we are looking for smartphones which are red in color

So the above URL is read as

“Hey Mr.Server, I would like to GET a list of smartPhone where the brand is apple and the color is red”

if you don’t specify what you are looking for, the server will return every smartphone it has but when you specify your requirements via query string parameters, the server only returns what you are looking for.

I hope this helped you in understanding what query string parameters are in a GET request.

Querystring
Mobile App Development
Swift Programming
API
Api Development
Recommended from ReadMedium