{"id":8832,"date":"2020-09-24T17:26:33","date_gmt":"2020-09-24T17:26:33","guid":{"rendered":"https:\/\/www.askpython.com\/?p=8832"},"modified":"2022-07-07T14:11:54","modified_gmt":"2022-07-07T14:11:54","slug":"flask-rest-api","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/flask\/flask-rest-api","title":{"rendered":"Flask REST API &#8211; Set up Guide for Your Flask Application"},"content":{"rendered":"\n<p>In this article, we will get familiar with APIs and REST APIs and then later build up our very own Flask REST API Application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is an API?<\/h2>\n\n\n\n<p>API or Application Programming Interface provides an interface to interact with different applications. Using APIs, we can<strong> retrieve, process,<\/strong> and <strong>send data<\/strong> <strong>&#8211; values <\/strong>from other applications.<\/p>\n\n\n\n<p>For example, consider a Web application. A client sends in some data as a request, the server then processes it, and then sends the appropriate response data back to the client. <\/p>\n\n\n\n<p>This whole process of interaction is done via the API. So let&#8217;s take a look at this example again and see where the APIs role is.<\/p>\n\n\n\n<p>Hence, what the client sends is an API containing the request data to the server. The server processes it and then sends back another API containing the appropriate response data to the Client.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CRUD operators and HTTP methods<\/h2>\n\n\n\n<p>While using an API, the Client can send request data to the server in many ways. These types are called <strong>CRUD<\/strong> Operations.<\/p>\n\n\n\n<p>Each CRUD (<strong>Create Retrieve Update Delete<\/strong>) operator has an HTTP method associated with it. <\/p>\n\n\n\n<p>Let us look at them now:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>GET HTTP Method\u2013 <\/strong>Retrieves specific information (for viewing purpose) from the server.<\/li><li><strong>POST \u2013 <\/strong>Sends\/Creates new information in the Server Database.<\/li><li><strong>PUT \u2013 <\/strong>Edits\/Updates information in the Database or else adds it if not present already.<\/li><li><strong>DELETE \u2013 <\/strong>Deletes information from the Server Database.<\/li><\/ul>\n\n\n\n<p>Okay, now that we know the methods, let&#8217;s understand when to use each of them. Consider the following example:<\/p>\n\n\n\n<p>We have a student database containing information like <strong>Names, Class, Age, <\/strong>etc.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To <strong>see<\/strong> the list of students &#8211; Use <strong>GET HTTP<\/strong> method<\/li><li>To <strong>add <\/strong>a new student information &#8211; Use <strong>POST HTTP<\/strong> method<\/li><li>To <strong>edit<\/strong> certain information( Class \/Age) of a student &#8211; Use <strong>PUT HTTP<\/strong> method<\/li><li>To <strong>delete<\/strong> a Student&#8217;s information from the Database &#8211; Use the <strong>DELETE HTTP<\/strong> method<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a REST API?<\/h2>\n\n\n\n<p>A REST (<em>Representational State Transfer<\/em>) API is similar to the standard API. Here when we send the server a request,  unlike an API which responds with data, REST API responds with<strong>\u00a0resources.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">REST API Resources<\/h3>\n\n\n\n<p>Now what are resources ?? <\/p>\n\n\n\n<p>Well, resources are data, but the way we see it changes. The resources are similar to <a href=\"https:\/\/www.askpython.com\/python\/oops\/object-oriented-programming-python\" class=\"rank-math-link\">Object-Oriented Programming.<\/a>  Let us understand it with the following example:<\/p>\n\n\n\n<p>Consider a Flask View &#8211; &#8220;\/Book\/Book123&#8221;<\/p>\n\n\n\n<p>Here, the same View Endpoint can perform 4 actions.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GET <strong>Book\/Book123:<\/strong> Shows the information about the &#8220;<strong>Book123.<\/strong>&#8220;<\/li><li>POST <strong>Book\/Book123<\/strong>: Creates a new book, &#8220;Book123.&#8221;<\/li><li>PUT <strong>Book\/Book123<\/strong>: Updates\/edits the information about the book &#8220;Book123.&#8221;<\/li><li>DELETE <strong>Book\/Book123: <\/strong>Deletes &#8220;Book123&#8221; from the database<\/li><\/ul>\n\n\n\n<p>Since a single entity has several functionalities( OOP methods), it can be thought of as a <strong>Book resource.<\/strong> <\/p>\n\n\n\n<p>Therefore, now the interaction between the Client and server is not with individual endpoint requests but with resources (with the same endpoint for different actions)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Statelessness of REST APIs<\/h3>\n\n\n\n<p>Another feature of the REST API is its statelessness. What this means is that after the server completes one action, it forgets about it.<\/p>\n\n\n\n<p>Let&#8217;s understand this with an example.<\/p>\n\n\n\n<p>Consider the Book Resource we saw above. Let&#8217;s say, I send data about a Book &#8211; &#8221; <strong>A Brief History Of Time <\/strong>&#8221; by Stephen Hawkings using the POST method. <\/p>\n\n\n\n<p>The server will add this information to the Database and then forget the action. That is next time I use a <strong>GET <\/strong>request to retrieve the Book; the server will not have any memory of the previous POST method action.<\/p>\n\n\n\n<p>The server will first go to the Database and search for the Book. And once it finds the Book, it will respond with that data. Again after it completes the action, it will forget about it.   <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use of JSON in Client-Server API Interaction<\/h2>\n\n\n\n<p><strong>APIs use JSON format <\/strong>for\u00a0<strong>accepting<\/strong>\u00a0and\u00a0<strong>returning requests<\/strong>. <\/p>\n\n\n\n<p>That is, the client sends the request data to the server as a JSON text. Similarly, the server processes the data and returns back the response again as a JSON text.<\/p>\n\n\n\n<p>Therefore, the whole process of a Web Application based on REST API is as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The user sends the request data to the server as JSON.<\/li><li>The server first converts the JSON into a python-readable format. <\/li><li>Then the server processes the request and creates the response data again as a JSON<\/li><li>Then the webpage Template converts the JSON response to a user-readable format and displays it on the webpage.<\/li><\/ul>\n\n\n\n<p>Therefore, real exchange of information between the client-side (FRONT-END) and the server (BACK-END) in API happens using<strong> JSON text. <\/strong><\/p>\n\n\n\n<p>JSON format is similar to a Python Dictionary:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{\n    &#039;student1&#039;:{\n        &#039;name&#039;:&#039;XYZ&#039;,\n        &#039;age&#039; : 21\n    }\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Installing POSTMAN<\/h2>\n\n\n\n<p>Postman is a collaboration platform for API development. Postman&#8217;s features simplify each step of building an API and streamline collaboration so you can create better APIs\u2014faster.<\/p>\n\n\n\n<p>Click here to download <a href=\"https:\/\/www.postman.com\/api-platform\/meet-postman\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">POSTMAN<\/a>. Once its installed, it will look like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"740\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/POSTMAN-window-1024x740.png\" alt=\"POSTMAN Window\" class=\"wp-image-7681\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/POSTMAN-window-1024x740.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/POSTMAN-window-300x217.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/POSTMAN-window-768x555.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/08\/POSTMAN-window.png 1159w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>POSTMAN Window<\/figcaption><\/figure><\/div>\n\n\n\n<p>Okay, coders! Enough with the reading, let us now start building our REST API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Building a Flask REST API Application<\/h2>\n\n\n\n<p>In this section, we will build a simple Book REST API application using the <strong>Flask RESTFul<\/strong> library. So let&#8217;s get started !!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Installing Flask_restful into your system<\/h3>\n\n\n\n<p>To install the Flask_RestFull package, run the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" class=\"rank-math-link\">pip command<\/a>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npip install flask_restful\n<\/pre><\/div>\n\n\n<p>Now that it is installed, lets move on to the Database part<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Coding the DB Models using SQLAlchemy<\/h3>\n\n\n\n<p>Here we will use SQLite Database to store our Models. To use them first install <strong>flask_sqlalchemy<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npip install flask_sqlalchemy\n<\/pre><\/div>\n\n\n<p>Now create a <strong>models.py<\/strong> file and add the following code<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom flask_sqlalchemy import SQLAlchemy\n\ndb = SQLAlchemy()\n\nclass BookModel(db.Model):\n    __tablename__ = &#039;books&#039;\n\n    id = db.Column(db.Integer, primary_key=True)\n    name = db.Column(db.String(80))\n    price = db.Column(db.Integer())\n    author = db.Column(db.String(80))\n\n    def __init__(self, name, price, author):\n        self.name = name\n        self.price = price\n        self.author = author \n    \n    def json(self):\n        return {&quot;name&quot;:self.name, &quot;price&quot;:self.price, &quot;author&quot;:self.author}\n<\/pre><\/div>\n\n\n<p>Here, the BookModel has a name, price, and author fields. Since the APIs are in JSON, we create an Object method <strong>.json()<\/strong> to return a JSON book object.<\/p>\n\n\n\n<p>We first have to instantiate a DB instance to create the DB model. Check out our <a href=\"https:\/\/www.askpython.com\/python-modules\/flask\/flask-postgresql\" class=\"rank-math-link\">SQLAlchemy Tutorial<\/a> if you have any doubts regarding SQLAlchemy. <\/p>\n\n\n\n<p>Now that we have our Models, lets now code the main Flask application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Coding the Flask Application<\/h3>\n\n\n\n<p>For the Flask REST API, we need to include an extra <strong>API(app) <\/strong>instance to indicate Flask that this is a REST API web app. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom flask import Flask\nfrom flask_restful import Api\n\napp = Flask(__name__)\n\napi = Api(app) #Flask REST Api code \n\nif __name__ == &#039;__main__&#039;:\n    app.run(host=&#039;localhost&#039;, port=5000)\n<\/pre><\/div>\n\n\n<p>Next, we need to give SQLite information to SQLAlchemy and link the DB instance (form models.py) with this application file.<\/p>\n\n\n\n<p>So for that, add the code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\napp.config&#x5B;&#039;SQLALCHEMY_DATABASE_URI&#039;] = &#039;sqlite:\/\/\/&lt;db_name&gt;.db&#039;\napp.config&#x5B;&#039;SQLALCHEMY_TRACK_MODIFICATIONS&#039;] = False\n\ndb.init_app(app)\n<\/pre><\/div>\n\n\n<p>Here, replace <strong>&lt;db_name><\/strong> with the name you want for your DB. <\/p>\n\n\n\n<p>SQLALCHEMY_TRACK_MODIFICATIONS is kept <strong>False<\/strong> just for simplicity.<\/p>\n\n\n\n<p>The third line links the DB instance with the app. We need the DB file in place so that webpage users can use it.  Also, we require that before the first user request itself.<\/p>\n\n\n\n<p>So to create the file we use the function:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n@app.before_first_request\ndef create_table():\n    db.create_all()\n<\/pre><\/div>\n\n\n<p>Add this below the above given codes itself. Okay with that in place, lets code our Flask REST API Resource Classes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Adding a Books List Resource<\/h3>\n\n\n\n<p>This resource would do the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GET method : Show the list of Books in the DB<\/li><li>POST method : Add a new Book information into the DB<\/li><\/ul>\n\n\n\n<p>So the code would look like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass BooksList(Resource):\n    def get(self):\n        #get all objects from BookModel\n        #return the JSON text of all objects\n        pass\n        \n    def post(self):\n        #convert the JSON data sent by the user to python-format\n        #create a new bookModel object and send in the data\n        #save to DB\n        pass\n<\/pre><\/div>\n\n\n<p>So the code should be able to do the tasks written above. Let&#8217;s now replace the comments with the actual code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass BooksList(Resource):\n    def get(self):\n        books = BookModel.query.all()\n        return {&#039;Books&#039;:list(x.json() for x in books)}\n\n    def post(self):\n        data = request.get_json()\n        new_book = BookModel(data&#x5B;&#039;name&#039;],data&#x5B;&#039;price&#039;],data&#x5B;&#039;author&#039;])\n        db.session.add(new_book)\n        db.session.commit()\n        return new_book.json(),201\n<\/pre><\/div>\n\n\n<p>Here in the GET method, <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Get the books present in DB using <strong>BookModle.query.all()<\/strong><\/li><li>Display JSON text of the Book one by one as a <strong>list<\/strong><\/li><\/ul>\n\n\n\n<p>In the POST Method,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Convert the JSON data, using <strong>request.get_json()<\/strong><\/li><li>Create and Add the new Book Information to the DB<\/li><\/ul>\n\n\n\n<p>That&#8217;s it; finally, we need to mention the URL endpoint for the BooksList Resource<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\napi.add_resource(BooksView, &#039;\/books&#039;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. Adding a Book Resource<\/h3>\n\n\n\n<p>Now, we will create a resource that will:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>GET Method: Display only a specific book requested by the user<\/li><li>PUT Method: Edit the information of the particular book. If not present, create one<\/li><li>DELETE Method: Delete the particular Book  <\/li><\/ul>\n\n\n\n<p>Here the code would look like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Book(Resource):\n    def get(self,name):\n        #get the book with the name given by the user\n        #if book exists, return it else return 404 not found \n\n    def put(self,name):\n        #convert the JSON data sent by the user to python-format\n        #Search if the book exists\n        #if it exists, update it with the data given by the user \n        #if does not exists, create and add the book into DB\n\n    def delete(self,name):\n        #Search if the book exists in the DB\n        #delete it\n<\/pre><\/div>\n\n\n<p>The code should be able to do all the above tasks. So add the code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Book(Resource):\n    def get(self,name):\n        book = BookModel.query.filter_by(name=name).first()\n        if book:\n            return book.json()\n        return {&#039;message&#039;:&#039;book not found&#039;},404\n\n    def put(self,name):\n        data = request.get_json()\n\n        book = BookModel.query.filter_by(name=name).first()\n\n        if book:\n            book.price = data&#x5B;&quot;price&quot;]\n            book.author = data&#x5B;&quot;author&quot;]\n        else:\n            book = BookModel(name=name,**data)\n\n        db.session.add(book)\n        db.session.commit()\n\n        return book.json()\n\n    def delete(self,name):\n        book = BookModel.query.filter_by(name=name).first()\n        if book:\n            db.session.delete(book)\n            db.session.commit()\n            return {&#039;message&#039;:&#039;Deleted&#039;}\n        else:\n            return {&#039;message&#039;: &#039;book not found&#039;},404\n<\/pre><\/div>\n\n\n<p>Here in the GET method,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>BookModel.query.filter_by(name=name).first()<\/strong> returns the first book it gets from the DB. It returns None, if nothing with the name was found.<\/li><li>Returns the JSON text of the Book  if found. Or else returns 404<\/li><\/ul>\n\n\n\n<p>In PUT method,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Convert the JSON data using <strong>request.get_json()<\/strong><\/li><li>Search for the book with the name.<\/li><li>If exists, replace the older data with the newly sent data<\/li><li>Or else create a new Book object<\/li><li>Add it to the DB<\/li><\/ul>\n\n\n\n<p>In DELETE method,<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Get the book with the name given by the user<\/li><li>Delete it<\/li><\/ul>\n\n\n\n<p>That&#8217;s it. Finally add the URL endpoint for this Resource<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\napi.add_resource(BookView,&#039;\/book\/&lt;string:name&gt;&#039;)\n<\/pre><\/div>\n\n\n<p>And we are done !!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Final Code of the Flask Rest API Application<\/h2>\n\n\n\n<p>Therefore, the combined Flask main application is given below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom flask import Flask,request\nfrom flask_restful import Api, Resource, reqparse\nfrom models import db, BookModel\n\napp = Flask(__name__)\n\napp.config&#x5B;&#039;SQLALCHEMY_DATABASE_URI&#039;] = &#039;sqlite:\/\/\/data.db&#039;\napp.config&#x5B;&#039;SQLALCHEMY_TRACK_MODIFICATIONS&#039;] = False\n\napi = Api(app)\ndb.init_app(app)\n\n@app.before_first_request\ndef create_table():\n    db.create_all()\n\n\nclass BooksView(Resource):\n    &#039;&#039;&#039;\n    parser = reqparse.RequestParser()\n    parser.add_argument(&#039;name&#039;,\n        type=str,\n        required=True,\n        help = &quot;Can&#039;t leave blank&quot;\n    )\n    parser.add_argument(&#039;price&#039;,\n        type=float,\n        required=True,\n        help = &quot;Can&#039;t leave blank&quot;\n    )\n    parser.add_argument(&#039;author&#039;,\n        type=str,\n        required=True,\n        help = &quot;Can&#039;t leave blank&quot;\n    )&#039;&#039;&#039;\n\n    def get(self):\n        books = BookModel.query.all()\n        return {&#039;Books&#039;:list(x.json() for x in books)}\n\n    def post(self):\n        data = request.get_json()\n        #data = BooksView.parser.parse_args()\n\n        new_book = BookModel(data&#x5B;&#039;name&#039;],data&#x5B;&#039;price&#039;],data&#x5B;&#039;author&#039;])\n        db.session.add(new_book)\n        db.session.commit()\n        return new_book.json(),201\n\n\nclass BookView(Resource):\n    &#039;&#039;&#039;\n    parser = reqparse.RequestParser()\n    parser.add_argument(&#039;price&#039;,\n        type=float,\n        required=True,\n        help = &quot;Can&#039;t leave blank&quot;\n        )\n    parser.add_argument(&#039;author&#039;,\n        type=str,\n        required=True,\n        help = &quot;Can&#039;t leave blank&quot;\n        )&#039;&#039;&#039;\n\n    def get(self,name):\n        book = BookModel.query.filter_by(name=name).first()\n        if book:\n            return book.json()\n        return {&#039;message&#039;:&#039;book not found&#039;},404\n\n    def put(self,name):\n        data = request.get_json()\n        #data = BookView.parser.parse_args()\n\n        book = BookModel.query.filter_by(name=name).first()\n\n        if book:\n            book.price = data&#x5B;&quot;price&quot;]\n            book.author = data&#x5B;&quot;author&quot;]\n        else:\n            book = BookModel(name=name,**data)\n\n        db.session.add(book)\n        db.session.commit()\n\n        return book.json()\n\n    def delete(self,name):\n        book = BookModel.query.filter_by(name=name).first()\n        if book:\n            db.session.delete(book)\n            db.session.commit()\n            return {&#039;message&#039;:&#039;Deleted&#039;}\n        else:\n            return {&#039;message&#039;: &#039;book not found&#039;},404\n\napi.add_resource(BooksView, &#039;\/books&#039;)\napi.add_resource(BookView,&#039;\/book\/&lt;string:name&gt;&#039;)\n\napp.debug = True\nif __name__ == &#039;__main__&#039;:\n    app.run(host=&#039;localhost&#039;, port=5000)\n<\/pre><\/div>\n\n\n<p>The <strong>models.py <\/strong> file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom flask_sqlalchemy import SQLAlchemy\n\ndb = SQLAlchemy()\n\nclass BookModel(db.Model):\n    __tablename__ = &#039;books&#039;\n\n    id = db.Column(db.Integer, primary_key=True)\n    name = db.Column(db.String(80))\n    price = db.Column(db.Integer())\n    author = db.Column(db.String(80))\n\n    def __init__(self, name, price, author):\n        self.name = name\n        self.price = price\n        self.author = author \n    \n    def json(self):\n        return {&quot;name&quot;:self.name, &quot;price&quot;:self.price, &quot;author&quot;:self.author}\n<\/pre><\/div>\n\n\n<p>Let us now run our server and check them using POSTMAN.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing the Flask REST API application using POSTMAN<\/h2>\n\n\n\n<p>Run the server and go to POSTMAN.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. BooksList Resource : POST method<\/h3>\n\n\n\n<p>Go to &#8220;<strong>\/books<\/strong>&#8221; endpoint using POST method<\/p>\n\n\n\n<p>In the body, select <strong>raw &#8211; JSON<\/strong> and add the JSON data in the body<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n{\n\t&quot;name&quot;:&quot;book1&quot;,\n\t&quot;price&quot;:123,\n\t&quot;author&quot;:&quot;author1&quot;\n}\n<\/pre><\/div>\n\n\n<p>Hit send<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"663\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-1-1024x663.png\" alt=\"POST\" class=\"wp-image-8860\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-1-1024x663.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-1-300x194.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-1-768x497.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-1-1536x994.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-1.png 1545w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>POST<\/figcaption><\/figure><\/div>\n\n\n\n<p>The Book &#8211; <strong>book1<\/strong> is created<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. BooksList Resource : POST method<\/h3>\n\n\n\n<p>Go to &#8220;<strong>books\/<\/strong>&#8221; using the GET method and hit send. You will get the list of Books in the DB<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"663\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-11-1024x663.png\" alt=\"GET\" class=\"wp-image-8861\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-11-1024x663.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-11-300x194.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-11-768x497.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-11-1536x994.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get-11.png 1545w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>GET<\/figcaption><\/figure><\/div>\n\n\n\n<p>Since we have only <strong>book1<\/strong>, it is showing only 1 object in the list.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Book Resource : GET method<\/h3>\n\n\n\n<p>Now go to &#8220;\/books\/<strong>book1<\/strong>&#8221; using GET and hit send<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"504\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get22-1024x504.png\" alt=\"GET\" class=\"wp-image-8862\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get22-1024x504.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get22-300x148.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get22-768x378.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get22-1536x756.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/get22.png 1545w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>GET<\/figcaption><\/figure><\/div>\n\n\n\n<p>See, it is showing the informatio of Book1<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Book Resource : PUT method<\/h3>\n\n\n\n<p>Go to &#8220;\/books\/<strong>book1<\/strong>&#8221; using PUT method and in the body, add the following JSON.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n\t&quot;price&quot;: 100,\n\t&quot;author&quot;:&quot;author123&quot;\n}\n<\/pre><\/div>\n\n\n<p>Since the name is already sent through the URL request, We need to send the price and author JSON. <\/p>\n\n\n\n<p>Hit send<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/put-1024x683.png\" alt=\"PUT\" class=\"wp-image-8863\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/put-1024x683.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/put-300x200.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/put-768x512.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/put-1536x1024.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/put.png 1545w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>PUT<\/figcaption><\/figure>\n\n\n\n<p>The price and author values have changed !! Try to check them using the GET method as well.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Book Resource : DELETE method<\/h3>\n\n\n\n<p>Go to &#8220;\/books\/<strong>book1<\/strong>&#8221; using the DELETE method<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"504\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/delete-1024x504.png\" alt=\"DELETE\" class=\"wp-image-8864\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/delete-1024x504.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/delete-300x148.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/delete-768x378.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/delete-1536x756.png 1536w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/09\/delete.png 1545w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>DELETE<\/figcaption><\/figure><\/div>\n\n\n\n<p>See it is deleted!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That&#8217;s it guys! I hope you have gained enough knowledge about the Flask REST API framework. Do try out the above codes yourselves for better understanding.<\/p>\n\n\n\n<p>See you guys next time !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will get familiar with APIs and REST APIs and then later build up our very own Flask REST API Application. What is an API? API or Application Programming Interface provides an interface to interact with different applications. Using APIs, we can retrieve, process, and send data &#8211; values from other applications. [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":8865,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-8832","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-flask"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=8832"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/8832\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/8865"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=8832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=8832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=8832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}