FastAPI Tutorial - Building RESTful APIs with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys assalamu alaikum welcome to amigos code in this video i'm going to teach you how to use this amazing python framework for building apis fast api was nominated as the open source python library of the year and to be honest it's quite amazing because you can start building your own apis in a matter of seconds so it kind of competes with frameworks such as flask and django and to be honest it's quite cool so in this video what you're going to learn is how to create this entire api right here where we're going to have a model called user and the user will have first name last name middle name gender and a list of roles and you'll see that this data then we'll store it into a fake database and you'll see how easy it is for us to build all of these http methods get post delete and put where the client will send a request and then we basically have the server right here so fast api uses the uvicon http server and it's very similar to express.js and node if you have done some javascript and also it kind of competes with golang as well because it's an asynchronous server so yeah so what we're going to do is we're going to have the client and this could be any ui or any other application that wants to connect to your back end and you're going to learn how to build all of this so if i show you and right here i'm going to issue a request to localhost 8000 and you can see that this is basically a json blob that we have but also under so in here under forward slash and then api v1 users have a look so all of this is actually coming from our back end build with fast api if you're new to my channel go ahead and subscribe give me a thumbs up so i can keep on recording this video so literally take two seconds and smash that like button so i can keep on recording these videos if you're not part of the amigo squad community go ahead and join the community is growing and i would love to see you there both on discord and the private facebook group without further ado let's kick off this video what exactly is fast api well fast api is nothing but a framework for building apis it kind of resembles frameworks such as express and others in the golang world but basically what they're trying to do is to bring some of that good stuff from those frameworks into the python world so in here you can see that they say it's very fast and high performance fast to code you can reduce about 40 percent of bugs in here very intuitive easy short robust and there is a set of standards already that you can just follow in order to build your apis so literally as you see in this course we're going to build a fully fledged api in a matter of seconds and it's going to be really fast hence the name fast api so in here in this main documentation so here you can actually go and read more about it um some of the options uh the installation process on and so forth but i will come back to this in a second now fast api is just a framework for building apis in order for you to serve your api you need a server so for that it uses it uses uv cone and uv con is the lightning fast asgi server and sgi stands for asynchronous service gateway interface and basically it's intended to provide a standard interface between async capable python web servers frameworks and applications so in this course you'll see that we'll use uv cone under the hood but in here this is how you can install uv cone and configure the ports and you'll see some of this stuff as well when we start learning about fast api so right here you see that they even have the logo right here and what's really cool about fast api and uv cone is that they are ready for production literally they are ready for production if you want to build production grade applications that real users will receive traffic you can definitely use a fast api to build your products next let's go ahead and install fast api and you'll see how we're going to build an api in a matter of seconds all right let's begin our journey building this entire application right here where we're going to use fast api and python so as i said fast api is just a framework for building apis and we still need the uv cone http server which is a very lightweight asynchronous server for python so if i go back to the docs and in here you can see that they have the installation process so pip install fast api and we also need to install the server right here right so for production they recommend us to use uv cone or hypercon but we're going to use uv cone in this course so let's together install fast api and uv cone and build a really simple api so for this course i'm going to use vs code so open up vs code and within vs code i do have this diagram which you can find the link for the repo where you can find the source code including this diagram so it's this one that you are seeing right here so let's together open up the terminal within vs code so new terminal and to install fast api is as follows so you say pip and then i've got pip 3 or you can just use pip and then install fast api and i also want to install uv cone so within quotes i'm going to say uv corn and then here i want the standard so this will bring the minimal dependencies for uv corn so here i've got a typo so this should be uv corn just like that press enter and there we go successfully installed fast api as well as uv cone now let me close this terminal for a second and in here i'm going to open up the explorer create a new file this will be named main dot py now within main.py i'm going to say from and then fast api import and then fast api next what i want to do is to create an instance of the application so i'm going to say app equals to and then fast api and if you've done some express js this will look very familiar now what i need to do is let me just use this annotation so i'm going to say at and then app dot get and this will give me a route for a get request so here i need to specify the path so here i'm just going to say within quotes for slash so this will be the root and then here i'm going to have a method call it root and inside it's not going to take anything and right here i'm going to return this object or dictionary and i'm and then i want to say hello and then here i'm going to say world and it's as simple as this now let me save this and open up the terminal and to run the application we say uvcorn and then main column and then amp so main is the name of our module so main.py and then app is the instance of our application that we've just defined and finally what i want is to say dash dash reload so if i make any changes the server will reload automatically so now press enter and have a look so we'll watch for changes in these directories so this is the directory that i'm working in and then have a look uv cone running on localhost 8000 and then at the very end you can see application startup completed so now what i can do is if i open up my web browser and in here let me just say localhost and then 8000 press enter and have a look so this is quite cool so i've got this json blob which is hello and then world so here if i so if i just close this and if i change this to hello and then mundo and then if i save this and then go back reload have a look i didn't even had to stop and start the server because when we ran it we told it to so here we told it to so have a look we told it to reload and to be honest this is it and you can see down at the bottom we have the logs so this was a get request to the root and the status code was 200 which means okay before we move on let me touch on http request methods and this will make sure that you understand exactly what you're doing moving forward now http requests defines a set of request methods to indicate the desired action to be performed for a given resource so we have a list of them so if i scroll down and you can find this link under the description of this video but have a look we have a get request and this says the get method requests a representation of the specified resource requests using get should only retrieve data in here so if i go back to vs code have a look when we say app dot get so this right here is our get http method and currently this is the path so this is root right so uh localhost a thousand and then the data that we return is this json object that contains a key hello and then the value mundo so if i go back to my web browser what i want to show you is in here if i uh right click and then inspect and you can do this in any web browser and go to network and reload the page and have a look what we have we have status method domain file here this is a get request to localhost a thousand and this was the path so forward slash i can click on it and this gives me more information about it right so this was a gate request the status was 200 okay this was the http version that was used this was the response headers and request headers in here i can have a look at the cookies the request but more importantly if i click on response we have the json object in here so if i click on raw so you don't have if you don't have the json extension you might see something like this but this is the response which was sent by the server so this response in here so this is pretty much how it works and something which is really important is so if i go to headers have a look the status is 200 which means okay so if i change this to forward slash and then foo enter so we have the status 404 meaning that it's not found because we haven't configured this route just yet so if i close this and if i go back to localhost 8000 we have hello mundo so this is a get request but we also have other http methods we have head so asks for a response identical to a get request but without the response body post this is used to submit an entity to a specified resource so you learn about post put is used to replace all current representations of a target resource so usually this is used for updating data in your back end delete the delete method is used to delete a specified resource connect options trace patch so on and so forth so i'm gonna leave a link where you can read more about this but it's very straightforward and in this course we're going to cover the most important ones which are get post put and delete before we carry on let me still teach you about an important concept that you should really be be aware when working with fast api and uv core so as i said before the asgi specification and if i click on this right here so this takes me to this documentation and an sgv and sgi stands for asynchronous servicer gateway interface right here and is intended to provide a standard interface between async capable python web service frameworks and applications now what this means is if i open up vs code so if you are aware of the async await keywords in either javascript or python then you will understand this sometimes we want to have this method to be able to wait for an asynchronous computation so the way we do it is by using async and then let's say here we have to wait for an http request so we can say await and then here this is like full for example right now obviously we don't have this method full that will go off and get some data from the internet using asynchronous fashion but if you want to use the await keyword inside of your methods you need to have this keyword before the dev keyword for your methods now this is really important and this is why this right here is an asynchronous service gateway interface now inside of the documentation for fast api they have a section on concurrency and a sync await so i'm going to leave this link where you can basically go and read more about it but async await is not something that we're going to fully cover in this course but i just want to make sure that you are aware of it so that when you see the a sync keyword used in methods then you know what it means so for this course we not going to need the await inside here maybe later but at this point i'm going to use the async keyword for all of my methods if you have any questions please do let me know otherwise let's carry on all right so for our application we want to be able to store a list of users into our database and then expose the api so that we can get users get user by id delete users update users so on and so forth so this is the model that we want to have right here you can see all the attributes for the user id first name last name middle name which is optional gender and a list of rows so let's together define this in our application so back to vs code and in here let's create a new file and i'm going to name this as models.py and for this course we're going to use pydantic so pydantic is a python library to perform data validation we can declare the shape of our data as classes with attributes so let's define our class so in here let's just say class this will be user and this will extend base and then model and in order for us to use base model we have to import so let's say from pi and then dantic import base and then model now inside of our class we can define the attributes so i said that we want an id and the id will be optional so let's just say optional and this will give me the import from typing optional and finally the data type for this will be a uid and you could choose for example an integer if you wanted to but in my case i'm going to say you it and if it's not provided i'm going to say uid and then v4 right here and make sure that you have the correct imports but vs code is adding these for me next i want to have the first name and this will be a string let's do the same for last name i also want to have middle name and this will be optional and finally i want to have the gender and this will be an enum so let's just have that so this we need to import so from enum import and then enum and now i can say class and this will be gender gender we need to pass the string and then enum type and this will have two values so male equals two and then male and followed by female there we go now to use it we just say gender in here so this is our type and let's do the same for roles so a user can have one or more roles so here this will be rows instead of oh actually role instead of gender this will be admin and then maybe you want the user role and maybe you also have a student role but this really depends on the type of application that you are building there we go and to use the row we need to say roles so this will be a list of type and then row and we need to import list right here from typing we also want the list and it's as simple as this so now that we have our model right here and we actually are using pi dantic and if you want to learn more about pydantic you can follow this link where uh you can learn more about the data validation and the settings that you can use but basically we haven't done much in terms of validation but you can use pyraminting to do all kinds of things to make sure that you enforce the correct validations so what we have now is we have our user right here that we can now store in our database and use it as a data store for our application next let's build this database right here which we're going to start off with a list but if you want to change to a real database that will be really straightforward as well next let's go ahead and build our database that will store the list of users so we have the user model now let's build this database where we can store users so let's go back to vs code and in here i realized that instead of models.py line 16 for the id we need to invoke this guy right here so just like just like this so we invoke so that we get a uid if it's not provided now let's open main.ui and inside after the application so line three let's go ahead and have the database so i'm going to say db and this will have a type of list and i'm going to import list and the type of the list will be user and also import user and this will contain a couple of users inside oops not warning but users so if i just scroll up have a look we have the imports so list from typing and models from users so this is the user that we've just created now inside let's have a couple of uses so here i'm going to say user and inside let's have one male and one female so here i'm going to say id and then this will be uid and then version four let's invoke this let's also define the first name equals two and let me put this full screen there we go so this will be jamila and the last name will be ahmed and let me put these on your line so this is much easier for you to read now here let's define the gender equals to and then gender dot and then female and i'm missing a comma here so female and we need to import gender so from here we import gender and we also need roles so here let's just say that jamila she is a student right so we have roles have a look admin user and student so here i can say square brackets and inside i'm going to say row dot and inside we have student and i need to import role as well just like that and there we go so let's have a comma here and let's have a second user and this will be alex and then jones and he's a male and let's say that alex is an admin and also a regular user so dot and then user so you can fine-tune these roles um to according your application but here i'm just trying to demonstrate how to use a list and there we go now one thing to bear in mind is that the id will change every time you restart the application but that is fine we're going to fix this in a second so this now is our database have a look so we have the database it's a list of users and currently it contains two uses so let me also remove this comma and we are good to go next let's define a route to give us all the uses that we have in this list and expose it to our clients all right so we have the database which is currently a list we also have the model now let's have an endpoint where clients can send a get request and we're going to use fast api to send data that we have stored in our list so let's open up vs code and in here so you've learned about http methods so again we need the get method so here at and then app dot and then get and here we need to specify a path so let's say forward slash api forward slash v1 forward slash and then users now let's have the async and then death fetch uses and inside we're not going to pass anything and now we can say return and then db so basically we're just returning all the contents in our db so basically it's a list of objects now let's test this so i'm going to save this and then open up the terminal and have a look the application reloaded and started completed and started successfully now if i open up my web browser and here before remember we have localhost 8000 and this says hello mundo and if i reload this still works but now if i say forward slash and then api forward slash v1 forward slash and then users press enter have a look so we get an array of objects and here check this out we have id so this is a random string or a uid first name jamila ahmed female and then rose she's a student we have alex so have look alex jones the gender is male and he has two rows so you can see that this is really cool now the reason why my output is formatted like this is because i'm using an extension for firefox so here i think it's called json viewer so i think it's this one here and basically it formats the output as uh you see in here so go ahead and install this and it's also available for chrome opera and other web browsers but to be honest this is it so you can see that this is really cool now one problem that we have is check this out so this id ends with 6d now if i add a space and then save this and then go back reload have a look the id will change so now is six two so what i wanna do is to have these ids fixed so i'm just gonna grab this string go back close this and in here so instead of us generating the u width like this let's just say u it and then inside we can pass the u it as a string let me get the other id command c and then instead of generating each time i'm going to say uid and then pass the id inside save this if i go back now if i reload check this out the ids are fixed and to be honest this is it so you've successfully managed to expose an endpoint that gives you back a list of uses if you have any questions please do let me know otherwise let's carry on okey dokey so we've managed to send a get request into our api and we get back a list of users now how do we add a new user so in here let's together have yet another api endpoint and the whole purpose of it is to add a new user so after the app.get so let's have yet another endpoint so we're going to say app dot and then if you recall from the http video so if i show you again in here so we have get which is used to retrieve data we have post submits an entity to the specified resource often causing a change in state or side effects on the server so here the resource is the end point that we're going to define in a second and then it submits an entity so we want to submit a new user so let's do that so here i'm going to say post and the resource is the same in here so you can have the exact same resource because the methods change so this is a get and this is a post but they live under the exact same path so now inside i'm going to say async def and then register underscore and then user and this will take a user so i'm going to say user and the type of it will be user so the user that we've defined now this user is the entity and this is basically what we received from the request body and i'm going to show you this in a second so now what we want to do is just say db dot append and we want to append the user so the user that is sent and as we know the id is generated and what i want to do is return and here i'm going to say user underscore id and then oh actually let me just say id so id and this is equal to user dot and then id just like that so we basically register a new user and then we send to the client the id for that user so now that we have the post request we can't really test post requests by using our web browser so from our web browser we can only send get requests next let's learn how to send a pulse request to our api right we have our method that takes the user from the request body and adds it to our database and then it returns the id back to the client but as i said we can't really test both requests with our web browser well we have two options one is so if i go to plugins so in here or extensions i can search for rest and then client so here we have a bunch of rest clients and this one called thunder client is one which recently came out and it has five stars so people seem to love it so we can install it there we go and in here if i close this i should have the client here so i've just clicked on these three dots and then thunder and then client and my font is actually quite big so that's why you can't really see it so if i just make it smaller there we go and let me increase the font right here so now you can see that we have the thunder client rest client so if i click on it now i can send a request so have a look i can say new request and basically i can send a post request get request so have a look get post put delete so let's just send a get request into our application so that you see it works so here i'm going to say http forward slash column column local host column 8000 forward slash api forward slash v1 forward slash users and if i send have a look we get back a json or an array of objects so this is the exact same thing that you see in here but now we are using this rest client but we can also send a post request so the post request looks like this so we have to copy one of these objects and here let's just say new request this will be a post and the url will be the same so http v1 and then uses and now inside of the body in here this is the json content that i want to send to my server so if i paste that and remove that comma there so this now is the json object that i'm going to send so let me just make this bigger like so so you can see everything and right here so remember from our model called user we don't have to send the id so i'm just going to remove that and here let's think about someone so here let's just say rita and then oliver so i'm just thinking about random names so anna just like that and right here she's also a student so this is the payload which is going to be sent to our server now this right here is the request body so this is the request body so have a look body right and then the format of it is json now remember so in here in our main.py have a look so let me just collapse this for a second have a look so this right here accepts a user and this is what comes from the request body so the user has to have the exact same shape as our model right here so id which is optional first name last name middle name optional gender and a list of roles now let me go back to this request and i can just say send now watch what happens i send the request and we have a status code so 200 means okay so this means that we successfully added this resource or this entity to our database so if i open up thunderclient let's send a get request so i'm just going to use this one here and before so this was the response with two users so i've looked two users but now if i send the exact same request there we go now watch we have jamil ahmed we have alex jones but we also have rita oliver anna she's a female and a student so this was really cool so you can see that how we used this client to send a post request the body was this json object then in our endpoint here so if i collapse this once more we got the user from the user body and then we added to our database and it seems like i accidentally removed this return id and then user id so let's just add it back let me save this and let me just go back in here and if i send a get request so you can see that i only have two users because every time the application restarts i lose the data but now let me send the post request so reta again send and now have a look so 200 but in the response we have id and then the id that was generated for reta now let me send the request again to get all users and have a look we have rita in here so this is pretty cool if you have any questions drop me a message otherwise let me show you yet another way that we can interact with our api so as you saw thunder client is really cool and powerful and we can basically interact with our api through vs code right so let me just uh for now just close this tab in this tab and what i want to show you is yet another powerful way that we can interact with our api so if i open up my web browser and in here you saw that this is our data coming from the back end and if i reload i think yeah we do have retail in here and that's because i haven't restarted the server so in here go ahead and remove api and then v1 uses and then type forward slash and then docs now check this out so here you can see that we have this interactive api documentation which is provided by swagger and this is given to us by default right so whatever endpoints we have it will generate the documentation for our api now what's really cool about it and let me just zoom out a little bit so here let's check this out so here we have default and this is because we can actually group these routes but currently we're not so we have root forward slash we have api v1 users which is a get request we also have post request that allows us to register a user and right here we have the schemas so we have gender role and user and we also have http validation error and validation error which are given to us by default so if i open up user it tells me the properties of a particular user and what fields are required so first name last name gender and roles are required and id and middle name are not so you can see that this is really powerful now i said this is an interactive api documentation what do i mean well if i click on get have a look it says it takes no parameters the responses media type and right here and it has an example value with schema but what i want to show you is i can click on try it out and then i can say execute and now have a look so this was the curl request that was generated so if you were to take this curl request and paste it in your terminal this would give you back a list of people and you can see the request url but more important have a look 200 and then we have the response body and we have three people so we have jamila alex jones and rita oliver so you can see that this is really cool so if you want you can interact with your api this way another way that you can view the documentation for your api is so here let me just collapse that and this is actually given by default right and it's interactive so i didn't show you actually so let's try and post someone so it gives me the example here but let me just say try it out and the id i'm going to remove it just like that and then first name i'm just going to give things as default for now and then just say execute we have the curl that was generated the request url and then 200 this was the response body so this is the id that was generated and you can see that it works so now i can collapse this say get all users execute and then check this out so here now we have four users so we have this one right here which was the last one now finally what i want to show you is so this is the interactive documentation provided by swagger but we also have redoc so redoc is just the api documentation it's not interactive so have a look we have all the endpoints so root fetch uses register user and basically my font is quite big but if i just make it smaller so you can see have a look so it gives me root fetch users and then register user and it gives me the schema have a look first name required last name so on and so forth and it gives back the responses you can have a look at the payload you can copy the payload and the url is right here and you can also download this open api specification so this is really awesome so there we go i've just showed you two ways that we can interact with our api we can use this one right here or we can use within vs code thunder client and to be honest they are both the exact same thing but this one has a bunch of more information about your api if you have any questions please do let me know otherwise let's build another api endpoint we can get all uses we can add users now let's learn how to delete a user from our database through the api so let's open up vs code and in here let us say at and then app dot and then delete so let me show you in here http we have delete used to delete a specified resource now the way that we're going to delete a resource is as follows so inside let's have the exact same path so let me just copy this just like that but instead what i want to do is say forward slash and then curly brackets and then user underscore and then id now this is a path variable and i'm going to show you this in a second so now let's have a sync death and then delete user and this will take the user and the score id the type of it is a uid and let's have the following logic to delete a user so here what we need to do is to loop through our database so i'm going to say 4 and then user in and then db if user dot id equals to the user id which is passed by the path variable if this is the case then what i want to do is say db dot remove and then pause the user and it's as simple as this now i need to return so this basically comes out of the loop as soon as we find the user now there is something else that we have to do here but i'm going to cover that in the next video but for now let's just save this and let's learn how to delete a user so here let's use the swagger docs so here let me just reload and you can see that we have delete let me make this bigger and let's fetch all the uses so try it out execute so we have two users right let's try and delete alex so let's grab this id command c and let me collapse this delete and it gives me the parameters so have a look i need to specify the user id so i'm going to say try it out pass the user id and now execute and have a look so this was the curl command but have a look at the request url so localhost 8000 api v1 users forward slash and this is the path variable and this is the id that we get from here this is the id that we get from here now you can see also we have a 200 status code so if i basically get all users so get execute you can see that now alex is gone and we only have jamila in our database let me show you how to do the same with thunderclient so here thunderclient and i'm going to say new request this will be a delete and we need the path so api if you want users and i need to get the id for jamila so let me just get there we go so that's the id and let me go back to this new request paste it in and then send 200 let's get all the users send and have a look no one is in our database because we deleted everybody now one thing which is not working quite well that is if i come back here for a second and i try to delete so i try to delete the same person so with the same id send have a look this is telling me that it's okay 200 but i know for a fact that we don't have anyone in our database right so we need to handle this scenario in here and that's what we're going to learn next so you've just saw that if i send this delete request this will always return a status code of 200 saying that the request indeed worked well that is true but that is not really what we want to do because with http these status codes they represent something so in here let me show you so in this link which you can find under the description of this video it says http response status codes and indicates whether a specific http request has been successfully completed and right here it's grouped into five classes so informational which is between 100 and 199 successful 200 299 redirection 300 399 client errors 400 and 499 and then we have server errors which is 505.99 so this is when something happens so this is when something goes wrong in our server this is when the client sends a bad request for example and this is for success as you've seen 200 and if i scroll down so you can see that it's telling me all these requests so what they mean so have a look successful responses 200 the request succeeded and it gives you more information but what i want to show you is so if i scroll down we have 400 client error responses so the server could not understand the request due to an invalid syntax so this is a 400 401 so this is unauthorized so probably you've seen this and we have 403 which is forbidden but maybe you've seen this one quite often 404 not found the server cannot find the requested resource so in our case we know that this user is no longer present in our system therefore we should return a 404 back to our client saying that the user cannot be found so how do we do that with fast api let's carry on on the next video so let's go back to vs code and let me just open up so i'm going to close this tab and i'm going to open up main.py put this full screen and right here so you saw that if we find the user we delete the user right here and then we return so return means just pretty much exit out of this function now what i want to do is so after the loop if this condition is not met i want to raise a http exception and we have to import http exception from fast api so here http exception and i don't think we need this uid so let's just get rid of it and if i scroll down so here inside we have to pass a couple things one is the status code and this is a 404 comma and then the detail so this is the message that the client should see so in our case i'm going to say f and then i'm going to say user with id column and then pass the id so user id does not exist and if i save this so you can see that this now makes more sense right so if the user doesn't exist then we should tell the client what has happened instead of just saying every time okay that your request was deleted so now if i open up this tab and if i send this delete request have a look what's going to happen so instead of 200 now we get 200 and that's because the server restarted therefore we have the data back but now if i send it a second time check this out so you see that user with id and then this is the id does not exist and this time we have a 404 not found so this is exactly what we want so i'm going to leave this link for the http status codes so you can go and read more about them but really all you need to remember is that there are five categories so in here informational success redirection client and server responses this is all for this video if you have any questions please do let me know otherwise let's carry on okie dokie soho time for your very first exercise so you've seen that fast api is really straightforward and awesome and you can build apis in a matter of seconds now i want to challenge you where i want you to implement the port method and put method allows you to update an existing resource in your database so what i want to do is go ahead and create a model that allows you to update the first name last name middle name and rules and make sure that all of them are optional then create a new route with at app dot put and then using the same path that we've been using and then receive that model and then go ahead and write some code that if any of those fields are not none then update the existing user right and also you need to make sure that you use path variables i'm going to leave the instructions under description of this video so that you can follow along but go ahead and challenge yourself and i'm going to give you the solution in the next video all right so for this solution what i've gone ahead and done was i created this class called the user update request that extends base model and first name last name middle name and roles are optional then inside of main.ui i do have this method called updateuser and the annotation that i've used was app.put the same path for the other ones and here i've got a path variable then i take the user update and the class is user update request and also the user id from the path then what i do is i check whether the user is in our database so using a for loop in here for user in db if the ids match then all i'm doing here is i'm checking whether the update the first name is not none so if that's the case then we update the first name i do the same for user update last name middle name as well as the rows and at the very bottom line 61 i do return because everything was fine otherwise if the user is not found i just raise http exception passing the status code 404 and the detail message user with id does not exist now let's test this out so here make sure that you have uv coin up and running so if i open up the terminal i'm gonna run this command because i stopped the server and let me close this now let's open up fast api and let's test this out so new and let me just copy the request from here so i'm going to grab all of this and if you don't remember so if i do a get request send you can see that we have uh two people right so let's update jamila ahmed in here so let's uh basically go to this new request and let me paste that and change get to a put this is jamila's ahmed right here so ending five six two five six two now the payload that i need to send to my new request is this one user update request first name last name middle name in rows so let me just copy this object in here command c and i'm going to go back to this tab and inside of body i'm going to paste this and for jamila so right here let me just put this bigger and i don't need to pass the id so gender as well and here let's just give her a middle name so let me just say hello for example so you see that it's been updated and for the roles let's also give her admin and let's just remove first name and last name right here and now if i send this request so let's send it and have a look we have a 200 status code which means okay and in here if i now get all uses you can see that we changed jamila middle name before was null now it's hello and also have a look at the roles right so she's a student as well as an admin so let's also change uh jamil ahmed to uh so here instead of saying jamil ahmed we'll just say we want to update the let's just say last name just like that to ali for example send and then go back and then have a look we updated the last name so you can see that this is working beautifully also if i basically change this to 3 we should get a 404 have a look 404 user with id ending in 563 does not exist and also if i add in a comma here and let me just say 2 and then send this so 442 so this is a bad request from the client so the range is within 400 so 422 and you can see that this is working beautifully if you have any questions getting this far please do let me know otherwise you can grab this source code from the description of this video that's all for now catch me on the next one i hope that you made this far if you enjoyed this video go ahead and give me a thumbs up next week what i want to do is i want to build the front end for this api so you'll see how to build a fully fledged full stack application using fast api as well as react also you saw that in here we kind of have a database which is a list but with what you have you can take the code and then change it to connect to a real database so i'm going to leave a link where you can go and learn about postgres so i've got a four hour course on postgres and i'm also going to leave a link where you can learn how to integrate fast api against a real database it should be really straightforward if you want to gain a certificate for this course go ahead and enroll on my website where i'll give you a certificate for free where you can show to employers if you're not part of the amigos code please go ahead and join the communities growing both this discord and the private facebook group and let me know what other videos that you want to see from this channel and that's all for now i'll catch you on the next one you
Info
Channel: Amigoscode
Views: 23,959
Rating: undefined out of 5
Keywords: amigoscode, fastapi, python tutorial, fastapi vs flask, fastapi authentication, fastapi mongodb, fastapi tutorial, restful api node js python, restful api explained, http get request python, http methods in rest api, http methods, Pydantic, uvicorn python, swagger documentation for rest api
Id: GN6ICac3OXY
Channel Id: undefined
Length: 63min 10sec (3790 seconds)
Published: Mon Nov 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.