0
0
Rest APIprogramming~5 mins

Resource identifiers in URLs in Rest API

Choose your learning style9 modes available
Introduction

Resource identifiers in URLs help us find and work with specific things on the internet, like a book or a user.

When you want to get details about a single item, like a user profile.
When you want to update or delete a specific resource, like a blog post.
When you want to link to a particular product in an online store.
When you want to organize data so each item has its own unique address.
Syntax
Rest API
/resource/{id}

The {id} is a placeholder for the unique identifier of the resource.

Resource identifiers usually come after the main resource name in the URL.

Examples
This URL points to the user with ID 123.
Rest API
/users/123
This URL identifies a book by its ISBN number.
Rest API
/books/9780140449136
This URL points to item 456 in order 789, showing nested resource identification.
Rest API
/orders/789/items/456
Sample Program

This small web app uses resource identifiers in the URL to get user details by their ID.

For example, visiting /users/1 returns Alice's data.

Rest API
from flask import Flask, jsonify

app = Flask(__name__)

# Sample data
users = {
    '1': {'name': 'Alice', 'age': 30},
    '2': {'name': 'Bob', 'age': 25}
}

@app.route('/users/<user_id>')
def get_user(user_id):
    user = users.get(user_id)
    if user:
        return jsonify(user)
    else:
        return jsonify({'error': 'User not found'}), 404

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

Resource identifiers should be unique within their resource type.

Use clear and simple IDs to make URLs easy to read and remember.

Nested resource identifiers help show relationships, like items inside orders.

Summary

Resource identifiers in URLs let you find and work with specific things online.

They usually come after the resource name and are unique IDs.

Using them makes your web addresses clear and organized.