Python API | Python HTTP Request And Response | Python Tutorial For Beginners | Edureka

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone this is vasim from erica and i welcome you all to this session in which i am going to talk about python apis so let's take a look at the agenda for this session so firstly i'm going to start with what exactly is an api and after that we will learn about the different types of requests and response codes that we have and moving further we will discuss how we can make an api request and to finally sum up this session we will run a custom api to send and receive data i hope you guys are clear with the agenda also if you are new here don't forget to subscribe to director for more exciting tutorial and press the bell icon to get the latest updates on any record and if you're looking for a certification program link is given in the description box below also tell us in the comment section guys if you are new to python and what sort of concepts you are looking for and what sort of tutorials do you want us to do next so moving on let's talk about what exactly is an api a python api is nothing but an application programming interface which is also the full form of api that is used to send and receive data on a server and it is primarily used to manipulate data and server using external script and a simple example would be how spotify twitter etc have their web apis for the users to retrieve data using the internet so now to you know approach this in a simplified way we can call api as an interface to any product or you know that is being you know deployed on a server where we can have an external program write a script and manipulate the data in a way where we are actually receiving or updating the values there so let's say for example twitter has a web api what we can do now here to retrieve and to send data we will have an external script and using that web api we'll be able to retrieve the data from over there so that is how apis work generally and normally there are a few reasons why we should go for you know an api at all the first one is so when your data set is too large you should go for an api so that every time you need data you are able to retrieve or update the values from the data set using the api and the second reason would be your users need data so let's say you have a server which is deployed and so a lot of users need your data and in order to do that you must have an api that they can interact with retrieve data from and simultaneously use it on their own platforms or whatever work they need it for and after that the third one would be data keeps updating or changing so let's say if you have a product or there is a server that i has to keep update and keep changing so you should have an api for that so that those values whatever you have to update or change and you know you can keep updating them using the web apis this is all about the api in general so this is exactly what i'm going to show you guys how we are going to make an api and we're going to use a flask web app for that and we're going to make a web api from that particular app and we'll show how we can retrieve data from the api in a json format etc so moving further in this session let me talk about what are http requests so before moving on we must be aware of terminologies like url and there are terminologies like json http so what exactly are these terminologies so url stands for uniform resource locator it's basically an address so whatever you know on your web browsers the address that you put in the section where you want to go to a particular website that particular address is also known as a url and it plays an important role while making web api because url is basically the primary source of you know locating any resource and after that we have http hypertext transfer protocol which basically serves as the means of primary communication for any server and then we have json which is also known as javascript object notation which is basically nothing but a data format on the internet which is both readable by the computer and the humans it's quite efficient for both of us so that's why we use json and there are other formats as well there are xml format that we can use now let's talk about a few requests and the most common ones or the most common http requests are get and post request so a get request is used to retrieve data from the resource and post request is used to update or add new data through the server so in simple language okay i've already told you what exactly is an http which is you know hypertext transfer protocol and it's just used to enable communication between the server and the clients clients here will be a user let's say so let's say if you want to you know convey or get some value from the server what you will do is use an http get request and it will locate the resource and get the values or the data from the resource basically in the form of a data format which is readable to you which can be a json format or an xml format as well on the other hand if you want to send some value or send some data to the resource through the server what you can do is you can use a http post request where you can just send any data through the server to the resource and that is how get and post work the other http requests include a put and delete so put basically is a lot similar to a post method only but there's only one difference it is used to send data to a server and create an or update a resource but the difference is that put requests are idempotent which means that every time you call a post request there'll be a new entry in the resource so let's say if your resource is a database and if you're sending data to the database through a post request every time you call that request there will be an entry in the database but for a put a request every time you execute that put request only one entry will be there in the database so that is how put and post requests differ from one another and then we have the delete method of course which is used to delete a specified resource and yes so this is all about the http request that we should know about and after that we have the response status codes guys so for each request that we make there are certain status codes guys so let's say if i want to execute a get request so there'll be a status code associated with it so the status quo element in the server response is basically a three digit integer which tells us about these status which can be a failure a success or there'll be some sort of indication that there has been an error it will redirect you to somewhere else or it might just tell you there is an error on server side there will be an error on the client side so basically if a status quo starts with four it will be a client error if it starts with five it will be a server error in case of a success it starts with the two normally 200 and if it's an informational status code which basically means that request has been received and the process is continuing it will start with a one if there is any redirection then it will start with three which basically means further action must be taken in order to complete the request so whenever we are making an api request we have to make sure that the status code is always 200 so that means that the request has been successfully made and that is what we should aim for in case if we have any errors we can you know basically solve them based on how we will pursue it based on the kind of error it is now we have actually discussed the response status quotes as well let's try to go to pycharm and we'll see how we can make a simple api and we're going to use flask so i'll just quickly tell you how you can install flask and everything so let's go to python guys so i've already created a project over here and i'll just tell you what you should do so since we are also using a database integration in this we are going to have to install mysql client and flask mysqldb with flask so these are the three things that you have to make sure that you have installed make sure you have made a virtual environment so that there's no errors while you're executing these commands so this is the primary api that i have created using flask so i'll just quickly tell you all the you know functions or whatever i've used over here i'll explain the code to you so if you are not new to flask you know exactly how you can start an application so what you do is you write from flask import flask after that you give the app or you know define the object over here and then quickly you can just define a app.route which is nothing but a url for your app and here i'm using this index method which is basically rendering the template using this render template method and i have index.html file over here okay i'll just show you what index.html looks like which you will have to make inside another templates directory and if you want to start with class guys we have a flask login tutorial that you can follow so there you can just learn how you can create a flask application with the multiple urls add user authentication there we have used the database integration as well so this is index.html guys where we have the title as home and we have just added a paragraph you know we just want to make sure that we are getting something so when i run this i'll explain the other code after this so we'll just run this debug is true so now we are opening this local host web server on the port 5000 and we have just added a paragraph over here so this is exactly what a typical flask application looks like in the beginning if you're a beginner so this is a very basic flask application where you have just added a paragraph from an html file now to create an api what we have done here or i'll just explain quickly so this is basically a list of dictionaries where i have course id and course name associated with it and i've saved it in a courses variable so this is basically the list now on my initial route or the initial url i am rendering this template index.html but in order to access the api i have another url guys which is this particular url and what i have done here so if you access this url what i'm doing is i'm using this jsonify method and whatever i have in courses i'm converting in into json format and showing it on my server so i copy the url that i want over here as you can see as a user if you access this url which is basically api over here you get this particular data in the format that is readable to both which is in the json format so this is how we get the data from the api and this particular link which i've just pasted here but it would be hidden of course if you are making a request over there so let's just test to do the request also so this we have created just to check if you are able to make a get request or not so if we make a get request we have the status quo 200 which means that the get request was successful and after that we are getting the data as well so this is how you make an api request on an api and now let's also see if we can pass arguments in the request or not so let's say if i'm going to pass an argument in the api over here and let's say i want to pass id is equal to 1 all right so this will be my argument and now what i have to do here is i have to check if id is there in the request dot arguments so whatever argument is there in the request if there is an id over there i'll check if the id that we are getting inside on that particular argument we'll change it to integer and after that we have an empty list which goes by the name result and then we check for each value inside our list which is also a dictionary of course so we check if the course id is equal to the id that we are getting inside the argument and if it's there so we are just um appending that particular result in the empty list and we return the jsonified result all right so let's check for that particular request as well right so we have changed it over here let's see if you're getting the answer and yes so if the course id is one the course name is python programming certification course that we have over here inside this so if we change the id to let's say three is it there three yes three is there so the answer would become or the data that we get the jsonified data will return python web development certification so this is how you can you know create an api and make request using arguments as well and now we'll take it one notch higher we will do the database integration as well so to do that okay so let's just stop the server here and close it all right so first thing that you have to do is set up mysql workbench on your system and start a connection from there okay we'll just close this over so while we are opening a mysql workbench i'll just quickly explain the code to you guys everything is similar guys so instead of that piece of code where we had courses as a list of dictionaries we are going to integrate the database when we will fetch the data from the database over here so for that you have to use the app.config with where you have to specify host name there will be a user password and the database name which i have already specified over here secret key you can also create which you should always create if you are using it on a production server and after that everything else is same there's only one thing that you have to import here which is flask mysqldb from that you have to import mysql and to basically make the app and after that you have to import mysqltp from which we are going to use the cursors to make changes to a database right guys so this is my app over here db so this is basically how i initialize the database and now we'll move on to the part where we write the code so basically what happened here so we are using the get methods over here it is basically like a default request so even if you don't specify it it would work now the first thing that i have done over here in the function is i have used a cursor so using that cursor i am going to execute a command in my database and before that let me just connect so we are in mysql workbench and let's take a look at the table that we have created which is courses over here all right once again guys so from login dot courses right so we already have two columns which has course id and course name and in course id it's an integer number and in the course name we have specified some character values but basically we are the names of the certification programs that is python programming certification data science certification machine learning and artificial intelligence based on the number of their course id all right so we'll go back to the code here and now this is the command that we are executing using the cursor which is select star from courses so everything that we are have over there we're going to select from the course and we are using this cursor.fetch all to fetch all the information over there and whatever information that we are getting from the cursor we are jsonifying it just like we did in the last program where we just sonified the results before returning it from the function and this is how easily it would work so let's just run this and go to the server all right so let's copy that url and we are getting the same results so python programming certification program data science certification program machine learning certification program and artificial intelligence certification program everything is correct so this is how we have specified i actually you know mentioned po here in the lower case that we have a sense of clarity that we are getting the same results in a way now one more thing so this is how i have got all the data from the database so what i want you to do here is pass an argument in the url just like we did in the last program where we mentioned the id and based on that we got the results so this is like a small assignment for you guys so that you understand how it really works let's just test the api request so that everything is fine server is already running so which is testing and the status code is 200 guys so we have successfully made or created a custom api and this is a very simple basic example how we create an api and this is actually not how an api is created this is a very basic example and we have made api request to get the data from servers we did not really post anything but if you check out our tutorial on flask login page or python login system there we have used the post request as well where you get the data from the users and then post them on the uh you know database that you have integrated so that is how you create an api in python guys and with this we have come to the end of session so if you have any questions you can mention them in the comment section below and tell us in the comments also like what you liked about this session and what else do you want us to cover in the upcoming sessions and if you are new here don't forget to subscribe to erica for more exciting tutorials and press the bell icon to get the latest updates on edureka and do check out eureka's python programming certification program link is given in the description box below thank you and see you guys in the next session i hope you have enjoyed listening to this video please be kind enough to like it and you can comment any of your doubts and queries and we will reply them at the earliest do look out for more videos in our playlist and subscribe to edureka channel to learn more happy learning you
Info
Channel: edureka!
Views: 97,071
Rating: undefined out of 5
Keywords: python api, python api get request example, python rest api call tutorial, rest api tutorial for beginners, restful api, Rest api tutorial, python rest api tutorial, rest api python, what is api, rest api explained, http response, http request, python requests, python rest api client, python, python tutorial, python tutorial for beginners, python programming, python training, python programming tutorial, edureka, python edureka, Python for beginners, Python api development
Id: KM_9in6ei50
Channel Id: undefined
Length: 17min 40sec (1060 seconds)
Published: Sun Nov 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.