0% found this document useful (0 votes)
13 views10 pages

EXP3

The document outlines the steps to create a simple REST API using Python and Flask, covering the installation process and the implementation of GET, POST, PUT, and DELETE operations. It includes a sample program and instructions for testing the API using Postman. The experiment concludes with successful execution and verification of the API functionality.
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)
13 views10 pages

EXP3

The document outlines the steps to create a simple REST API using Python and Flask, covering the installation process and the implementation of GET, POST, PUT, and DELETE operations. It includes a sample program and instructions for testing the API using Postman. The experiment concludes with successful execution and verification of the API functionality.
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
You are on page 1/ 10

EX.

NO: 03 Create simple REST API using python for following


DATE: operation a.GET b. PUSH c. POST d. DELETE

Aim:
To create a simple REST API using python to do the GET, POST, PUT and DELETE operations

Algorithm:
Step 1: Start
Step 2: Install Flask
Step 3: Start the Flask App
Step 4: Use Postman to Test Endpoints
Step 5: View Server Output
Step 6: Stop
Procedure:
Step1: Installation of python and click on the “use admin privileges when installing py.exe ”,”Add
python.exe to PATH”.
Step2: Select customize installation and click next.

Step3: In advanced options click on “install python 3.13 for all users” and click install
Step4: In setup progress installation in process.

Step5: Successfully install in python and click on the close button.


Step6:To verify the python version in command prompt and enter “python --version (or) py --version”.

Step7: To install the package of flask enter “pip install flask”

Step8:open python (or) IDLE enter the code and save the code. (ex. “api.py”)
Program:
from flask import Flask, jsonify, request
app = Flask(__name__)
# Sample data
data = [
{'id': 1, 'name': 'Item 1'},
{'id': 2, 'name': 'Item 2'},
{'id': 3, 'name': 'Item 3'}
]
# GET request to retrieve all items
@app.route('/items', methods=['GET'])
def get_items():
return jsonify({'items': data})
# GET request to retrieve a specific item by ID
@app.route('/items/<int:item_id>', methods=['GET'])
def get_item(item_id):
item = next((item for item in data if item['id'] == item_id), None)
if item:
return jsonify({'item': item})
else:
return jsonify({'message': 'Item not found'}), 404
# POST request to add a new item
@app.route('/items', methods=['POST'])
def add_item():
new_item = {'id': len(data) + 1, 'name': request.json['name']}
data.append(new_item)
return jsonify({'item': new_item}), 201
# PUT request to update a specific item by ID
@app.route('/items/<int:item_id>', methods=['PUT'])
def update_item(item_id):
item = next((item for item in data if item['id'] == item_id), None)
if item:
item['name'] = request.json['name']
return jsonify({'item': item})
else:
return jsonify({'message': 'Item not found'}), 404
# DELETE request to remove a specific item by ID
@app.route('/items/<int:item_id>', methods=['DELETE'])
def delete_item(item_id):
global data
data = [item for item in data if item['id'] != item_id]
return jsonify({'message': 'Item deleted'}), 200
if __name__ == '__main__':
app.run(debug=True)
Step9: After run the code, the output will be display in command prompt.

Step10: Install the postman in official website

Step11: open the postman and click on the “Run as administrator”.Then click “continue with google” enter
the email ID.
Step12: After login this site is trying to open postman ,click on the check box and click
open.

Step13:Welcome page in open postman


Step14: GET Request to Retrieve All Items
 Set the request type to GET.
 Enter the URL: http://127.0.0.1:5000/items
 Click "Send."

Step15: GET Request to Retrieve a Specific Item by ID:


 Set the request type to GET.
 Enter the URL for a specific item ID, for example: http://127.0.0.1:5000/items/1
 Click "Send."
Step16: POST Request to Add a New Item
 Set the request type to POST.
 Enter the URL: http://127.0.0.1:5000/items
 Go to the "Body" tab, select "raw" and choose "JSON (application/json)". Enter the request body
 Click "Send."

Step 17: PUT Request to Update an Existing Item


 Set the request type to PUT.
 Enter the URL for a specific item ID, for example: http://127.0.0.1:5000/items/1
 Go to the "Body" tab, select "raw" and choose "JSON (application/json)".
 Enter the updated information.
 Click "Send."
Step 18: DELETE Request to Remove a Specific Item by ID
 Set the request type to DELETE.
 Enter the URL for a specific item ID, for example: http://127.0.0.1:5000/items/1
 Click "Send."

Step19: View Server Output

Result:
Thus, the experiment to create a simple REST API using python to do the GET, POST, PUT and
DELETE operations is executed and verified successfully.

You might also like