RESTful APIs with Python and Flask : Part 2

 Hello Friends,

Welcome back !!!

In my last blog we saw that how we can setup a RESTful API using Python and Flask framework.

We also learned how to pass request params from client to the API call and receive it at server side.

Continuation on the same, today we are going to learn how to create POST API call and pass the JSON request to it.

We'll use a tool call POSTMAN to hit the POST API. If you don't have it, you can install it from below link -

https://www.postman.com/downloads/

Ok, let's get started.

I am assuming that you have already gone through my earlier blog and have created the directory structure for this project. If not, then please go through it and create the app.py file. 

https://diwakarsinhalearn.blogspot.com/2021/02/restful-apis-with-python-and-flask-part.html

Step 01 : In the app.py file, please add the below code.

@app.route("/employee", methods=["POST"])
def employee():
data = request.get_json()

if data:
name = data['name']
age = data['age']
department = data['department']
designation = data['designation']
salary = data['salary']

return("Hello, " + name + "!!! You are " + str(age) + " years old, " + " works in department "
+ department + ", with designation " + designation + " and having salary " + str(salary))

Step 02 : Save this app.py file.
Let's try to understand what above code does.
I have created a method called employee() and defined routing on it. Also, it is mentioned that what kind of HTTP request, this routing is going to handle (In this case, it is POST).
Client will pass the data in the request body of the API call, in JSON format. 
request.get_json() method will return the data which was passed from the client call and keep it as a Python dictionary.
All the individual fields are getting stored in different variables by accessing the dictionary.
At the end of it, same information is getting returned as response to the client.
Step 03: Run the app.py by executing the 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 04 : Start the POSTMAN tool and open a new request window. Type below URL in the address bar of it -


http://127.0.0.1:5000/employee


Step 05 : On the request window of the POSTMAN, you can find the body tab. Select it and check the request type as raw and corresponding data type as JSON from the drop down.


Step 06 : Put below text in the body of the request and hit Send button next to address bar, in the request window -


{

"name" : "Bob",

"age" : 30,

"designation" : "SSE",

"department" : "Security",

"salary" : 50000

}


You will get below as the output -


POST method call


That's it !!!

Please leave a comment if you face any problem while running this tutorial.

I'll try to reply on that as soon as possible.


In our next tutorial we'll learn how to serve HTML content using python and Flask.


Comments

Post a Comment

Popular posts from this blog

RESTful APIs with Python and Flask : Part 01