Path Parameters in API call

Path Parameters in API call

HTTP Request parameters are used in API calls to identify resources and filter the resource. As part of the API call parameters are sent along with the endpoints for different purposes. Parameters are very important in designing and using the REST APIs.

Path Parameter

Path parameters are used to identify or locate a specific resource. Path parameters are separated by '/' from the endpoint.

https://gorest.co.in/public/v2/users/{id}

In the above URL, to the endpoint '/users', the '/{id}' path parameter is appended. It helps, to retrieve the user information with the given ID.

Passing Path Parameter in the API call

The base URL or URI is 'https://gorest.co.in/public/v2'

Endpoint is '/users'

The path parameter is '/5632444' - in this case id=5632444

  1. Passing path parameter by adding it to the URL.
import requests
from pprint import pprint

class TestPathParametersDemo:
    # https://gorest.co.in/public/v2/users/{id} - sample url with path parameter
    BASE_URI = 'https://gorest.co.in/public/v2'
    ENDPOINT = '/users'
    PATH_PARAMETER = '/5632444'

    # sending path parameter by adding to url
    def test_sending_path_parameter(self):       
        request_url = self.BASE_URI+self.ENDPOINT+self.PATH_PARAMETER
        response = requests.get(request_url) # sending api request by adding the BASE URI, Endpoint, Path parameter
        print(f"\nRequest status code is {response.status_code}")
        data = response.json()
        pprint(data)
  1. Passing path parameter as a dictionary. Using a dictionary multiple path parameters can be sent.
import requests
from pprint import pprint

class TestPathParametersDemo:

    # https://gorest.co.in/public/v2/users/{id} - sample url with path parameter
    BASE_URI = 'https://gorest.co.in/public/v2'
    ENDPOINT = '/users'

    path_params = {
        'id':5632444
    }
    # sending path parameter using dictionary
    def test_sending_path_parameter(self):        
        request_url = self.BASE_URI+self.ENDPOINT
        response = requests.get(request_url, params=self.path_params) # sending api request by adding the BASE URI, Endpoint, Path parameter
        print(f"\nRequest status code is {response.status_code}")
        data = response.json()
        pprint(data)

Console output

test_path_parameters_demo.py::TestPathParametersDemo::test_sending_path_parameter 
Request status code is 200
[{'email': 'devar_keerti@leffler-ziemann.example',
  'gender': 'female',
  'id': 5632444,
  'name': 'Keerti Devar',
  'status': 'inactive'}]
PASSED

This is all about path parameters from my side. Thank you for reading.

Did you find this article valuable?

Support QA Automation Enthusiast by becoming a sponsor. Any amount is appreciated!