0% found this document useful (0 votes)
6 views4 pages

Understanding REST APIs Explained

REST (REpresentational State Transfer) is an architectural style for designing web services, introduced by Roy Fielding. A REST API allows communication between systems using HTTP methods to perform actions on resources identified by URLs. REST is popular due to its simplicity, language independence, statelessness, and scalability.

Uploaded by

freefirecsk1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Understanding REST APIs Explained

REST (REpresentational State Transfer) is an architectural style for designing web services, introduced by Roy Fielding. A REST API allows communication between systems using HTTP methods to perform actions on resources identified by URLs. REST is popular due to its simplicity, language independence, statelessness, and scalability.

Uploaded by

freefirecsk1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔹 What is REST?

REST stands for REpresentational State Transfer.​


It's not a protocol like HTTP, but a set of rules or architecture style for designing web
services.

It was introduced by Roy Fielding in his PhD dissertation.

🔹 Key idea behind REST:


Imagine you're talking to a server using simple HTTP methods (like GET, POST, PUT,
DELETE) to perform actions on resources (like users, posts, orders, etc.).

Each resource is identified by a URL (Uniform Resource Locator).​


For example:

GET [Link]

Means: “Hey server, give me the user with ID 101”

🔹 What is a REST API?


A REST API (or RESTful API) is an API (Application Programming Interface) that follows the
rules of REST.

It allows different systems (like a mobile app and a server) to communicate using HTTP
requests.

🔹 Example:
Suppose you have an online store API.

HTTP Method Endpoint Meaning

GET /products Get all products


GET /products Get product with ID 1
/1

POST /products Add a new product

PUT /products Update product with ID 1


/1

DELETE /products Delete product with ID 1


/1

🔹 Why REST is Popular?


●​ Works over HTTP (simple and widely used)​

●​ Language independent​

●​ Stateless (server doesn’t remember your previous request)​

●​ Easy to scale and cache​

CODE
API CALL
from flask import Flask, request, redirect, url_for, render_template,jsonify

app = Flask(__name__)

cart = []

@[Link]('/')
def index():
return render_template('[Link]', cart=cart)

@[Link]('/api/add', methods=['POST'])
def add_to_cart():
product = {
'name': [Link]['name'],
}
[Link](product)
return jsonify({'message': 'Product added to cart', 'cart': cart}), 201
return redirect(url_for('index'))

@[Link]('/api/delete/<string:product_name>', methods=['DELETE'])
def delete_from_cart(product_name):
global cart
cart = [product for product in cart if product['name'] != product_name]
return jsonify({'message': 'Product deleted ', 'cart': cart}), 201

return redirect(url_for('index'))

if __name__ == '__main__':
[Link](debug=True)

SIMPLE API
from flask import Flask, request, redirect, url_for, render_template,jsonify

app = Flask(__name__)

cart = []

@[Link]('/')
def index():
return render_template('[Link]', cart=cart)

@[Link]('/api/add', methods=['POST'])
def add_to_cart():
product = {
'name': [Link]['name'],
}
[Link](product)
return jsonify({'message': 'Product added to cart', 'cart': cart}), 201

return redirect(url_for('index'))

@[Link]('/api/delete/<string:product_name>', methods=['DELETE'])
def delete_from_cart(product_name):
global cart
cart = [product for product in cart if product['name'] != product_name]
return jsonify({'message': 'Product deleted ', 'cart': cart}), 201

return redirect(url_for('index'))

if __name__ == '__main__':
[Link](debug=True)

You might also like