CRC-Blogs

Passing JSON Data on Server using Flask and RestApi – Python

Before starting, you should know about RestApi and Flask. Rest is Representational state transfer framework used for development of web services. Web services that uses REST framework are known as RESTful Web Services, which provides interoperability between systems on the internet. RESTful Web Services allow the requesting systems to access and manipulation of data representation of web resources by using a uniform and predefined set of stateless operation such as GET, POST, REQUEST and DELETE. Flask is a lightweight WSGI web application framework. It is designed to get started quick with the ability to scale up to complex applications.

Pre-requisites:

Install Python and Flask, using following commands

# yum install python

# pip install flask

Steps:

After installing python and flask, create a virtual environment by using below mentioned steps:

# virtualenv envPY

# source envPY/bin/activate

This will show an output as:

(envPY) [root@localhost ~] #

Now, write the following python script by making a file named as code.py and save it in directory as “/root/envPY/

After writing the script, make a JSON file named data.json and write the following script in it.

Save these files in the folder named as “envPY” in root directory.

Now return to the terminal and run the python script using command:

# python code.py

Resulted output will be shown as:

On the web browser and write the ip as http://127.0.0.1:5000/. It will show the JSON data in the browser

Similarly, you can use the curl command in separate terminal for results, such as:

# curl http://127.0.0.1:5000/

Above mentioned command will show the following results in the terminal;