0
0
Rest APIprogramming~5 mins

Multiple filter parameters in Rest API

Choose your learning style9 modes available
Introduction

Multiple filter parameters let you ask for specific data by using more than one condition. This helps you get exactly what you want from a big list.

You want to find all books by a certain author and published after a certain year.
You need to get all users who live in a specific city and have a certain subscription type.
You want to list products that are both in stock and cost less than a certain price.
You want to search for events happening in a date range and at a specific location.
Syntax
Rest API
GET /resource?filter1=value1&filter2=value2&filter3=value3

Each filter is added as a separate parameter joined by &.

The server uses all filters together to narrow down the results.

Examples
Get books written by Rowling published in 2007.
Rest API
GET /books?author=Rowling&year=2007
Get users who live in Paris and have a premium subscription.
Rest API
GET /users?city=Paris&subscription=premium
Get products that are in stock and cost 50 or less.
Rest API
GET /products?in_stock=true&max_price=50
Sample Program

This small web app returns books filtered by author and year if given. You can try URLs like /books?author=Rowling&year=2007 to see filtered results.

Rest API
from flask import Flask, request, jsonify

app = Flask(__name__)

# Sample data
books = [
    {"title": "Book A", "author": "Rowling", "year": 2007},
    {"title": "Book B", "author": "Tolkien", "year": 1954},
    {"title": "Book C", "author": "Rowling", "year": 2005}
]

@app.route('/books')
def get_books():
    author = request.args.get('author')
    year = request.args.get('year', type=int)

    filtered = books
    if author:
        filtered = [b for b in filtered if b['author'] == author]
    if year:
        filtered = [b for b in filtered if b['year'] == year]

    return jsonify(filtered)

if __name__ == '__main__':
    app.run(debug=True)
OutputSuccess
Important Notes

Filters are combined with AND logic: all conditions must be true.

Order of parameters does not matter.

Always encode special characters in URLs (like spaces).

Summary

Multiple filter parameters help you get specific data by combining conditions.

Use & to add more filters in the URL.

The server returns only items matching all filters.