Lesson 24 of 158 – Path Parameters
24%

Path Parameters

Path parameters are values included directly inside the URL path of an API endpoint. They are commonly used to identify a specific resource such as a particular student, user, product, or course.

For example, in https://example.com/api/students/25, the number 25 can represent the ID of a specific student.

Note: Path parameters are part of the URL path. Unlike query parameters, they are not placed after a question mark.

1. What is a Path Parameter?

A path parameter is a dynamic value placed inside an API URL path to identify a particular resource.


/api/students/25

Here, 25 can be the ID of the student.

2. Basic Path Parameter Syntax

A common path parameter structure is:


/resource/{id}

For example:


/students/25

Here, 25 is the actual value of the ID parameter.

3. Path Parameter Example

Suppose the API contains a students resource.


https://example.com/api/students/10

The value 10 identifies a particular student.

4. Path Parameter vs Query Parameter

Path Parameter Query Parameter
/students/25 /students?id=25
Part of URL path Added after ?
Often identifies a resource Often filters or modifies a request

5. Student ID as a Path Parameter

A student ID is a common example of a path parameter.


GET /api/students/25

The API can use 25 to find the student whose ID is 25.

6. User ID as a Path Parameter

User APIs can use a user ID in the URL.


GET /api/users/15

The value 15 can identify one user.

7. Product ID as a Path Parameter

Product APIs can use product IDs in their URLs.


GET /api/products/100

Here, 100 identifies a particular product.

8. Course ID as a Path Parameter

A course management API can identify a course using its ID.


GET /api/courses/5

The API can return information about course 5.

9. GET with Path Parameter

GET is commonly used with path parameters to retrieve one resource.


GET /api/students/25

The server can return the student identified by ID 25.

10. PUT with Path Parameter

A PUT request can use a path parameter to identify the resource that should be updated.


PUT /api/students/25

The request body can contain the new student information.


{
    "name": "Rahul Kumar",
    "course": "TypeScript"
}

11. PATCH with Path Parameter

PATCH can use a path parameter when only part of a resource needs to be updated.


PATCH /api/students/25

Example request body:


{
    "course": "React Native"
}

12. DELETE with Path Parameter

DELETE commonly uses a path parameter to identify the resource that should be deleted.


DELETE /api/students/25

Here, the API can identify student 25 and delete that record.

13. Multiple Path Parameters

An API can have more than one path parameter when its resource structure requires it.


/api/students/25/courses/10

Here:

  • 25 can represent a student ID.
  • 10 can represent a course ID.

14. Nested Resource Path

Path parameters can be useful for nested resources.


/api/students/25/assignments

This can represent assignments belonging to student 25.

15. Path Parameter in React Native Fetch

React Native can dynamically add an ID to a Fetch URL.


const studentId = 25;

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

const data = await response.json();

console.log(data);

16. Path Parameter in Axios

Axios can also use a dynamic ID in the endpoint.


const studentId = 25;

const response = await axios.get(
    `https://example.com/api/students/${studentId}`
);

console.log(response.data);

17. Reading Path Parameters in PHP

The exact method for reading a path parameter in PHP depends on how the API routing is implemented.

For a simple PHP API, the URL path can be processed to extract the ID.


$url = $_SERVER['REQUEST_URI'];

$parts = explode('/', trim($url, '/'));

$id = end($parts);

For larger applications, a dedicated routing structure is usually more appropriate.

18. Validating a Path Parameter

A path parameter should be validated before it is used by the API.


$id = filter_var(
    $id,
    FILTER_VALIDATE_INT
);

if ($id === false) {
    // Invalid ID
}

This helps ensure that the API receives the expected type of value.

19. Path Parameter and Database Query

After validating an ID, a PHP REST API can use it in a prepared SQL statement.


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

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

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

$student = $stmt->fetch();

Prepared statements help prevent SQL injection.

20. Path Parameter and 404 Response

A requested resource may not exist.


GET /api/students/9999

If student 9999 does not exist, the API can return an appropriate error response, commonly using HTTP status code 404 Not Found.


{
    "success": false,
    "message": "Student not found"
}

21. Path Parameters and API Design

Clear resource-oriented URLs make APIs easier to understand.


/api/students
/api/students/25
/api/courses
/api/courses/10

The first URL represents a collection, while the second identifies a specific student.

22. Path Parameter vs Query Parameter Example

Path parameter:


/api/students/25

This can identify student 25.

Query parameter:


/api/students?course=react

This can filter students by course.

23. Path Parameter and Search

Path parameters and search parameters have different purposes.


/api/students/25

This identifies a specific resource.


/api/students?search=rahul

This can search a collection for matching records.

24. Testing Path Parameters in Postman

Postman can be used to test API endpoints containing path parameters.

  1. Open Postman.
  2. Select the HTTP method.
  3. Enter the API URL.
  4. Add the required resource ID.
  5. Click Send.
  6. Check the API response.

Example:


GET https://example.com/api/students/25

25. Dynamic Path Parameter from React Native

A student list can provide an ID when the user selects a student.


const openStudent = async (studentId) => {

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

    const data = await response.json();

    console.log(data);
};

The selected ID becomes part of the API URL.

26. Common Path Parameter Mistakes

  • Using the wrong resource ID.
  • Forgetting the ID in the URL.
  • Using a query parameter when a path parameter is required.
  • Passing an invalid ID.
  • Not validating the ID on the server.
  • Using an incorrect endpoint structure.
  • Forgetting URL encoding when appropriate.
  • Not handling a resource that does not exist.

27. Path Parameters and Authentication

A protected API can use a path parameter together with an authentication token.


GET /api/students/25

Authorization: Bearer YOUR_TOKEN

The path parameter identifies the resource while the authentication information controls access.

28. Complete Path Parameter Flow


React Native
      ↓
Select Student
      ↓
Student ID
      ↓
/api/students/25
      ↓
GET Request
      ↓
PHP REST API
      ↓
Validate ID
      ↓
MySQL Query
      ↓
JSON Response
      ↓
React Native

The path parameter carries the resource identifier from the client to the API.

29. Complete Student Path Parameter Example

Suppose a React Native application wants to display student number 25.


const studentId = 25;

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

const response = await fetch(url);

const student = await response.json();

console.log(student);

The server receives the request for the resource identified by 25.

30. Complete Path Parameters Concept

Path parameters are an important part of REST API design. They allow an API to identify specific resources directly from the URL path.


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

Endpoint:
students

Path Parameter:
25

Complete URL:
https://example.com/api/students/25

In a React Native application, the path parameter can be generated dynamically from the selected item's ID and sent to the PHP REST API. The backend can validate the ID, query MySQL, and return the requested resource as JSON.

📌 Key Points

  • Path parameters are values included in the URL path.
  • They are commonly used to identify specific resources.
  • Example: /api/students/25.
  • The value 25 can represent a student ID.
  • Path parameters are different from query parameters.
  • GET can use path parameters to retrieve a specific resource.
  • PUT and PATCH can use them to identify resources to update.
  • DELETE can use them to identify resources to remove.
  • Multiple path parameters can be used when necessary.
  • Path parameters should be validated by the backend.
  • PHP can extract path information depending on the routing design.
  • Prepared statements should be used when IDs are used in SQL queries.
  • React Native can dynamically create URLs using template literals.
  • Axios and Fetch can both work with path parameters.
  • Postman can be used to test path parameter endpoints.

🧠 Quick Quiz

Question: In the URL https://example.com/api/students/25, what does 25 typically represent?