Lesson 23 of 158 – Query Parameters
23%

Query Parameters

Query parameters are values added to the end of an API URL to provide additional information to the server. They are commonly used for searching, filtering, sorting, pagination, and controlling API responses.

Query parameters begin after a question mark ? in a URL. When multiple parameters are used, they are separated using an ampersand &.

Note: Query parameters usually do not identify the resource itself. Instead, they provide additional instructions or conditions for an API request.

1. What are Query Parameters?

Query parameters are key-value pairs added to an API URL after a question mark.


https://example.com/api/students?course=react

Here, course=react is a query parameter.

2. Query Parameter Syntax

The basic syntax of a query parameter is:


?key=value

Example:


https://example.com/api/students?course=react

The question mark starts the query string.

3. Question Mark in Query Parameters

The first query parameter is introduced using a question mark ?.


/api/students?course=react

Everything after the question mark is part of the query string.

4. Key and Value

A query parameter contains a key and a value separated by an equals sign =.


?course=react
  • course is the key.
  • react is the value.

5. Simple Search Parameter

A search parameter can be used to search for a specific value.


https://example.com/api/students?search=rahul

The API can use the search value to find matching students.

6. Filtering with Query Parameters

Query parameters are commonly used to filter API results.


https://example.com/api/students?course=react

The API can return only students belonging to the specified course.

7. Multiple Query Parameters

Multiple query parameters can be used in the same URL.


https://example.com/api/students
?course=react&city=patna

The first parameter starts with ? and additional parameters are separated using &.

8. Ampersand Separator

The ampersand & separates multiple query parameters.


?course=react&city=patna&status=active

This URL contains three query parameters.

9. Query Parameter for City

A city parameter can be used to filter records by city.


https://example.com/api/students?city=patna

The API can use this value when selecting matching records.

10. Query Parameter for Status

A status parameter can be used to request records with a particular status.


https://example.com/api/students?status=active

The server can use the value active to filter the response.

11. Query Parameter for Category

Products or courses can be filtered using a category parameter.


https://example.com/api/products?category=mobile

The API can return products belonging to the requested category.

12. Query Parameters and GET Requests

Query parameters are especially common with GET requests because they allow the client to specify which data it wants.


GET /api/students?course=react

The server reads the query parameter and generates an appropriate response.

13. Query Parameters in Fetch

React Native can include query parameters directly in a Fetch URL.


const response = await fetch(
    "https://example.com/api/students?course=react"
);

const data = await response.json();

console.log(data);

14. Building a Query URL with Variables

Query parameters can be created dynamically using JavaScript variables.


const course = "react";

const url =
    "https://example.com/api/students?course="
    + course;

console.log(url);

15. Query Parameters with Template Literals

Template literals make dynamic query URLs easier to create.


const course = "react";

const url =
    `https://example.com/api/students?course=${course}`;

console.log(url);

16. URLSearchParams

JavaScript provides URLSearchParams for creating query parameters in a structured way.


const params = new URLSearchParams({
    course: "react",
    city: "patna"
});

const url =
    "https://example.com/api/students?"
    + params.toString();

console.log(url);

17. URLSearchParams Output

The previous example produces a query string similar to:


course=react&city=patna

The complete URL becomes:


https://example.com/api/students?course=react&city=patna

18. Query Parameters with Axios

Axios provides a convenient params option for query parameters.


const response = await axios.get(
    "https://example.com/api/students",
    {
        params: {
            course: "react",
            city: "patna"
        }
    }
);

console.log(response.data);

19. Search with Query Parameters

A search screen in a React Native application can send the entered search text to the API.


const search = "rahul";

const response = await fetch(
    `https://example.com/api/students?search=${search}`
);

const data = await response.json();

The API can then return matching records.

20. Query Parameters for Pagination

Query parameters can also control which page of data should be returned.


https://example.com/api/students?page=1&limit=10
  • page=1 specifies the page.
  • limit=10 specifies the number of records.

21. Query Parameters for Sorting

An API can use query parameters to specify a sorting order.


https://example.com/api/students?sort=name

Another example can specify descending order:


https://example.com/api/students?sort=-name

The exact sorting syntax depends on the API design.

22. Reading Query Parameters in PHP

PHP can read query parameters from the $_GET superglobal.

For this URL:


https://example.com/api/students?course=react

PHP can read the value using:


$course = $_GET['course'] ?? '';

The variable $course will contain the requested value.

23. Multiple Query Parameters in PHP

PHP can read multiple query parameters from the same request.


$course = $_GET['course'] ?? '';
$city = $_GET['city'] ?? '';
$status = $_GET['status'] ?? '';

These values can then be used when building the API response or database query.

24. Query Parameters and MySQL

A PHP REST API can use query parameter values to filter database records.


$course = $_GET['course'] ?? '';

$sql = "SELECT * FROM students
        WHERE course = :course";

$stmt = $pdo->prepare($sql);

$stmt->execute([
    'course' => $course
]);

Prepared statements should be used when values are sent to SQL queries.

25. Encoding Query Parameter Values

Query parameter values may contain spaces or special characters. These values need to be encoded correctly when constructing URLs.


const search = "React Native";

const url =
    `https://example.com/api/students?search=${encodeURIComponent(search)}`;

Using URL encoding helps ensure that special characters are represented correctly.

26. Query Parameters in Postman

Postman provides a convenient way to test query parameters.

  1. Create or select a GET request.
  2. Enter the API URL.
  3. Open the Params section.
  4. Enter the parameter key.
  5. Enter the parameter value.
  6. Click Send.

Postman will add the query parameters to the request URL.

27. Common Query Parameter Mistakes

  • Forgetting the question mark before the first parameter.
  • Using a comma instead of an ampersand.
  • Using the wrong parameter name.
  • Forgetting to encode special characters.
  • Sending an empty or invalid value.
  • Assuming every API supports the same parameter names.
  • Not validating query parameter values on the server.
  • Building unsafe SQL queries directly from user input.

28. Complete Query Parameter Flow


React Native
      ↓
Search / Filter Input
      ↓
Query Parameters
      ↓
API URL
      ↓
GET Request
      ↓
PHP REST API
      ↓
Read $_GET Values
      ↓
Filter Database
      ↓
JSON Response
      ↓
React Native

Query parameters allow the mobile application to tell the API which data it wants.

29. Complete Query Parameter Example

Suppose a React Native application wants active React students from Patna.


https://example.com/api/students
?course=react&city=patna&status=active

The API receives three query parameters:

  • course = react
  • city = patna
  • status = active

The PHP API can validate these values and use them to retrieve the required records.

30. Complete Query Parameters Concept

Query parameters are an important part of REST API development. They allow a client application to send additional information to the server without changing the main resource endpoint.


Base URL:
https://example.com/api/

Endpoint:
students

Query Parameters:
?course=react&city=patna

Complete URL:
https://example.com/api/students
?course=react&city=patna

In React Native applications, query parameters are especially useful for search, filtering, sorting, and pagination.

📌 Key Points

  • Query parameters provide additional information to an API.
  • The first query parameter starts with ?.
  • Multiple parameters are separated using &.
  • Query parameters use key-value pairs.
  • GET requests commonly use query parameters.
  • Query parameters can be used for searching.
  • They can be used for filtering API results.
  • They can be used for sorting.
  • They can be used for pagination.
  • JavaScript provides URLSearchParams for creating query strings.
  • Axios provides a params option.
  • PHP can read query parameters using $_GET.
  • Query parameter values should be validated on the server.
  • Prepared statements should be used when query values are used in SQL operations.

🧠 Quick Quiz

Question: Which symbol starts the query string in a URL?