0
0
Rest APIprogramming~5 mins

Search parameter in Rest API

Choose your learning style9 modes available
Introduction

A search parameter helps you find specific information from a large set of data by telling the system what to look for.

When you want to find all users with a specific name in a database.
When you need to get products within a certain price range from an online store.
When filtering blog posts by a particular tag or category.
When searching for events happening on a certain date.
When retrieving records that match a specific status or condition.
Syntax
Rest API
GET /resource?searchKey=searchValue

The search parameter is added after the question mark (?) in the URL.

Multiple parameters can be added using the ampersand (&) symbol.

Examples
Finds users whose name is 'alice'.
Rest API
GET /users?name=alice
Finds products in the 'books' category priced at 20 units.
Rest API
GET /products?category=books&price=20
Finds events happening on June 1, 2024.
Rest API
GET /events?date=2024-06-01
Sample Program

This program sends a request to a web API to find users named 'bob'. It then prints how many users were found and their details.

Rest API
import requests

# URL with search parameter to find users named 'bob'
url = 'https://api.example.com/users?name=bob'

response = requests.get(url)

if response.status_code == 200:
    users = response.json()
    print(f"Found {len(users)} users named 'bob':")
    for user in users:
        print(f"- {user['name']} (ID: {user['id']})")
else:
    print('Failed to get users')
OutputSuccess
Important Notes

Search parameters are case sensitive depending on the API.

Always encode special characters in search values to avoid errors.

Check API documentation for supported search keys and formats.

Summary

Search parameters help filter data by specifying what you want.

They are added to the URL after a question mark (?).

Multiple parameters can be combined using &.