RESTful APIs with Python and Flask : Part 01

Hello Friends !!! 

Welcome to this blog in which we will learn how to create RESTful APIs using Python and Flask.

Python is a very simple programming language with lots of rich and powerful libraries to implement your business logic.

Flask is a Python web framework which provides the facility to expose the RESTful services.

In this tutorial we'll use this framework to implement the APIs.

PREREQUISITE -

Skill set needed for this tutorial : Basic Python understanding and familiarity with RESTful services

Python Setup : Python should be install. To verify if Python is install or not please run the below command on the console -

$ python --version

Python 2.7.10

Above will be the output when Python is install on your m/c.

Flask Setup : To install Flask please run below command on the console -

$ pip install Flask

Successfully installed Flask-1.1.2 ..............


Above will install Flask on your m/c.


IDE : Pycharm can be used as the IDE.


Let's get started.


SIMPLE GET REQUEST


Step 01 : Create a folder named src in any of your favourite locations in your m/c.

Step 02 : In the folder src create the file app.py

Step 03 : Put the following line of code in it -


from flask import Flask, render_template, request
app = Flask(__name__)
app.config["DEBUG"] = True


@app.route("/", methods=["GET"])
@app.route("/index", methods=["GET"])
def index():

return "Hello World !!!"

if __name__ == "__main__":
app.run()


In the above code snippet there are 3 parts, the import, the routing and the starting of service.


Part 1 : We import the necessary packages which are required to create a web application. A flask instance has been created and stored in the variable named app. We set the DEBUG mode to be true.


Part 2 : In this section we have defined the routing. It specify which type of RESTful request we want to handle and what will be the URL pattern that it will handle. Accordingly we have defined a method which will get executed once this URL pattern will be hit.


Part 3 : The Flask instance is started by calling its method run().


Step 04 : Run the app.py by executing below command in your console


$ python app.py


 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

 * Restarting with stat

 * Debugger is active!


You will get something like above in the console logs.


Step 05 : Hit the URL as mentioned in the above logs and you will get the below page in your browser.


Output



PASSING REQUEST PARAMETERS IN THE URL

Flask provides a request object which will help to get the parameters which are passed in the Form submission or in URL itself.

from flask import Flask, render_template, request
app = Flask(__name__)
app.config["DEBUG"] = True


@app.route("/", methods=["GET"])
@app.route("/index", methods=["GET"])
def index():
if request.args:
name = request.args.get("username")
return "Hello, " + name
else:
return "Hello, World !!!"

if __name__ == "__main__":
app.run()

In the above code snippet all is same as the first example except now we have a request object and that will have the arguments which are passed while hitting the URL.

Make the required changes in the app.py file and restart the application.

Hit the browser with below URL -

http://127.0.0.1:5000/?username=Diwakar

This will give us the below output

GET call with params

Please note that if you it the URL with the parameter, it will show the output as we get in the first example.

In the next tutorial we'll learn about POST request with form submission.


Comments

  1. Such a powerful blog. Right on point. Easy to understand. Thanks Diwakar.

    ReplyDelete

Post a Comment