5. REST - RESTful Web Services using Python
5. REST - RESTful Web Services using Python
1
| Rest Web Services using Python |
Characteristics
• No User Interface
▪ Web services do not have a GUI. They provide data and
functionality to other applications or services.
• Interoperability
▪ They are designed to be consumed by other applications,
regardless of the platform or language.
• Examples
▪ APIs for weather data, payment gateways, social media
integrations.
DIFFERENCE BETWEEN WEB APPLICATION AND WEB SERVICE
S.N Web Application Web Service
1. Purpose - Web applications are Purpose - Web services are
designed for human interaction. designed for machine-to-machine
It is a software application which communication over a network.
runs on a web server and is
accessed by a web browser
2. UI Interface – It has a user UI Interface – Usually it provides
interface (GUI) data and functionality without a
user interface
3. Usage - Web applications are Usage - Web services are
accessed via web browsers accessed via HTTP requests
(Ex. from other applications or
services)
• Both web applications and web services can be built using frameworks
like Flask in python, but they serve different purposes and are used in
different contexts.
2
| Rest Web Services using Python |
3
| Rest Web Services using Python |
Endpoints
• This provides CRUD operations (Create, Read, Update, Delete) via
HTTP methods (GET, POST, PUT, DELETE).
DIFFERENCES BETWEEN SOAP AND REST WEB SERVICES
S.N SOAP Services REST Services
1. SOAP stands for simple object REST stands for
access protocol (Structured representational state transfer.
protocol)
2. It is a protocol REST is an architectural style
3. It only supports XML format It supports various formats like
HTML, XML, JSON, Plain Text
etc,.
4. SOAP needs more bandwidth for its REST doesn’t need more
usage bandwidth
5. JAX-WS is the java API for SOAP JAX-RS is the java API for
web services REST
6. It is more secure than REST It is less secure than SOAP
7. Resources – XML & HTTP Resources – HTTP
8. It is used to transport data in XML Here it is used to transport
format data in JSON format which is
based on URI.
9. As it is XML based, it works with It works with HTTP GET,
WSDL (Web Service Description POST, PUT, DELETE methods
Language)
10. It can’t use REST because it is a REST can use SOAP web
protocol services and it can use any
protocols like HTTP, SOAP
4
| Rest Web Services using Python |
5
| Rest Web Services using Python |
6
| Rest Web Services using Python |
2. SOURCE CODE
(home.html)
<html>
<center>
<body>
<h1 style="color: #bc0024;">Welcome to REST WEB SERVICES
using Python Flask</h1>
<h4><a href="add">Click to get Add Service</a></h4>
<h4><a href="mul">Click to get Mul Service</a></h4>
</body>
</center>
</html>
(hello.py)
# load necessary modules for python web framework for REST web service
from flask import Flask, request, render_template
# create an object for Flask Web
app = Flask(__name__)
# HOME PAGE
@app.route("/")
def home():
return render_template("home.html")
# CONTENT PAGE 1 (Route for addition)
@app.route('/add') Create a route for the add() function
def add():
try:
# get two inputs from browser via Query String
a = int(request.args.get('a'))
b = int(request.args.get('b'))
result = a + b
7
| Rest Web Services using Python |
Alternatives Inputs
• Accessing request.args Directly - You can access the query
parameters directly from request.args which is a dictionary like object.
a=request.args[“a”]
b=request.args[“b”]
Where,
• a and b are user defined parameters.
• Using request.form – You can use request.form, if you are dealing
with form data (Ex. POST Request)
a=request.form.get(“a”)
b=request.form.get(“b”)
8
| Rest Web Services using Python |
a=request.values.get(“a”)
b=request.values.get(“b”)
Where,
• a and b are user defined parameters.
NOTE
• In flask web framework, a separate folder templates needs to created
manually in the current project directory to keep all design files like html
files, CSS files and many more.
9
| Rest Web Services using Python |
3. OUTPUT
3.1 HOME PAGE
10
| Rest Web Services using Python |
11
| Rest Web Services using Python |
12
| Rest Web Services using Python |
13
| Rest Web Services using Python |
PROJECT STRUCTURE
14
| Rest Web Services using Python |
2. SOURCE CODE
(home.html)
<html>
<center>
<body>
<h1 style="color: #bc0024;">REST WEB SERVICES using Python
Flask (HTML FORM-POST)</h1>
<h4><a href="addUI">Click to get Add Service</a></h4>
<h4><a href="mulUI">Click to get Mul Service</a></h4>
</body>
</center>
</html>
(add.html)
<html>
<body>
<center>
<h1>Inputs Submission for Addition</h1>
<form action="addCode" method="post">
Number 1:<br>
<input type="text" size="35" name="n1"/><br>
Number 2:<br>
<input type="text" size="35" name="n2"/><br>
<input type="submit" value="Submit"/>
</form>
</center>
</body>
</html>
15
| Rest Web Services using Python |
(mul.html)
<html>
<body>
<center>
<h1>Inputs Submission for Multiplication</h1>
<form action="mulCode" method="post">
Number 1:<br>
<input type="text" size="35" name="n1"/><br>
Number 2:<br>
<input type="text" size="35" name="n2"/><br>
<input type="submit" value="Submit"/>
</form>
</center>
</body>
</html>
(calc.py)
from flask import *
# create an object for flask app
obj=Flask(__name__)
# create a home route
@obj.route("/")
def home():
# connect home page for list of operations
return render_template("home.html")
# content route 1 - define route for add UI
@obj.route("/addUI")
def addUI():
return render_template("add.html")
16
| Rest Web Services using Python |
17
| Rest Web Services using Python |
3. OUTPUT
3.1 HOME PAGE
18
| Rest Web Services using Python |
19
| Rest Web Services using Python |
20