[go: up one dir, main page]

0% found this document useful (0 votes)
4 views7 pages

Cloud Computing Practical3

This document provides a step-by-step guide to creating a simple REST service using Flask, Python, and Postman. It includes instructions for setting up the environment, installing necessary tools, and writing code for endpoints to get and add items. Additionally, it demonstrates how to test the API using Postman with example requests and responses.

Uploaded by

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

Cloud Computing Practical3

This document provides a step-by-step guide to creating a simple REST service using Flask, Python, and Postman. It includes instructions for setting up the environment, installing necessary tools, and writing code for endpoints to get and add items. Additionally, it demonstrates how to test the API using Postman with example requests and responses.

Uploaded by

Kishore Kholan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Practical 3

Create a Simple REST Service


Software Tools Required:
 Code Editor: Visual Studio Code (VS Code)
 API Testing Software Application: Postman
 Framework: Flask (micro web framework)

Downloads Required:

 Python: Download Python | Python.org


 Postman: Download Postman | Get Started for Free
(Create Account/ Log in with Google)

After Downloading Python, Set the Path in Environment Variable in User Variables > Path >
Edit > New and Paste the Path for Python.

New version is 3.13, Here the version is 3.12 so following the same for new version.

Till Python312: Copy Directory Path


Start > Python 3.13.1 (64 bit) > Open File Location > Python 3.13.1 (64 bit)

Till Python312\Scripts: Copy the Directory Path


Start > Python 3.13.1 (64 bit) > Open File Location > Python 3.13.1 (64 bit) > Open File
Location > Scripts

Demonstration:

Check Version:
Python Programming Language

Step 1: Install Flask: Make sure you have Python installed on your system.
Open cmd and type the following command

pip install Flask


Demonstration:

Step 2: Create a folder name SimpleRESTService, Open with VS Code and create python
file.

Check and Install Extensions for Python

Extensions:

Python

Python Debugger
Filename: app.py

Code:
from flask import Flask, jsonify, request
app = Flask(__name__)

# Sample data
data = [
{'id': 1, 'name': 'Item 1'},
{'id': 2, 'name': 'Item 2'},
]

# Endpoint to get all items


@app.route('/items', methods=['GET'])
def get_items():
return jsonify({'items': data})

# Endpoint to get 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
# Endpoint 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({'message': 'Item added successfully', 'item': new_item}), 201

# Run the application


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

Output:
View>Terminal
Type
Path> python app.py
Demonstration:

Postman
API Testing Software Application to Test the API
Open Postman
# Endpoint to get all items
Send a GET Request:
URL: http://127.0.0.1:5000/items

Response:
{

"items": [

"id": 1,

"name": "Item 1"

},

"id": 2,

"name": "Item 2"

Demonstration:
# Endpoint to get a specific item by ID
Send a GET Request:
URL: http://127.0.0.1:5000/items/1

Response:
{
"item": {
"id": 1,
"name": "Item 1"
}
}
Provides specific id from Sample Data available i.e., URL: http://127.0.0.1:5000/items/1
Demonstration:

Throws Error when not item not found from Sample Data. Example
URL: http://127.0.0.1:5000/items/100
Demonstration:

# Endpoint to add a new item


Send a POST Request:
URL: http://127.0.0.1:5000/items

Body: Set the body to raw JSON and include:

{"name":"item3"}

Response:

"item": {

"id": 3,

"name": "item3"

},

"message": "Item added successfully"

Demonstration:

---------------------------------

You might also like