Fast API Tutorial, Part 16: Form Fields

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends and welcome to part 16 of our fast api tutorial in this video we are going to briefly touch on form fields um and what i mean by form fields has to do with the way you're passing data in in a post request or a patch or a put or something like that so let's just go ahead and get started app.post login we are going to create async def login and typically we would do something like user name string password string or we would go in here and say class user base model this is more like what we would do because this would actually create them as query parameters so we would do something like this let's go ahead and do hey it worked and we would say user is user return user so this is what we currently have okay and let's take a look at this really quickly so we can see we're passing in application json we hit execute and we get exactly this okay we've seen that many many times before but what we're going to do here is we're going to include the possibility we're going to assume the possibility that the user is passing in a different type of request not json but one where we're actually using a a form in html we're going to be passing in something called form url encoded as a possibility for a post request if you're sending in a form like that you can do multi-part form data encoding type so it doesn't have to be application json it can be these other types of um information that we're passing in so let's go ahead here and we're going to say username is a string and is going to be a form field password is a string and is also a form field and in this case we will just return username is username and we'll print password so that it doesn't uh you know my id doesn't yell at me now we need to import form from fast api and we hit save and we're going to get yelled at because we need to install another package pip install python multi-part what i'm going to do instead of just doing pip install i'm going to copy add it to my requirements file and i'll close this and now i will go into my terminal i'm not going to install it up here because not everyone's using pycharm i've got my virtual environment activated and pip install dash r requirements.txt we go i had already it was already cached so it was very easy to to install quickly now this is working it should be at least let's check out our run right here there we go we're good and here now we have a form that we're going to be passing and you can notice this is now x dash www dash form-url encoded so we are going to say username is username password is password and what i want to do let's just inspect the page network and let's just see what the request looks like let me close this up a little bit bring this back oh geez lou okay hold on a second let me i'll fix this i'll fix it wow this just looks terrible okay that's better execute let's click login and let's click the payload so we can see this is now a form as form data i'm going to do app.post login json and we'll quickly set up class user username string password string so i'm gonna oh base model there we go now let's refresh our page and we're going to clear this out because i want to look at the two of these side by side so this is going to be username password and let's hit execute and we see our login form data great grand now let's try this out execute log in json and you can see our request payload is our traditional json object that we're used to that we're used to seeing okay okay that's generally about it for this one this was another fairly short video um i i want to just mention really quickly you so you're going to use something like this if you are if you're going to be receiving data from an html form this is you know if you're going to be manually setting up you know a json object in the front end you know stringifying it using either fetch or axios i'm sorry about my knuckle cracking using fetch or axios then you're going to use the body attribute that we've already seen you know you can you can use like we can say user name string string equals body and password string equals body what is the matter with me return username so we refresh our page you can see it's the exact same thing that we saw before log in json it works the exact same way so this is this is another way that we can let me just kind of this is these are kind of like the two ways that we can that we can take a post request if you want to compare them side by side and we don't want to use this user class if we set it up as a body if we set the parameters up as body attributes then they will be sent back as json if we set them up as form data it will be sent back as a form object it's not going to like it if we try and do both not that he's not going to like it but it's just it's it's it's not i don't know how to say it it's let me try one more time it's going to create it as a form element regardless so if you pass this in here we're saying this is going to be json by passing it as a body well it's just going to overwrite it and it's going to pass it in as form data anyway that's that's what i was trying to say um you have to pick and choose one or the other okay that's kind of it sorry if i had a little bit of a brain fart for a second it's been a long day long week long year and it's only march 3rd my goodness okay um in the next video we are going to touch on um uh request files so if you you know if you're going to be uploading a file doing something like that now you can understand why you touched on form fields just now because when you upload a file you need to append it to a form that you've created in the client okay i will see you in that next video
Info
Channel: JVP Design
Views: 6,243
Rating: undefined out of 5
Keywords: fastapi
Id: PSJH-ixO-PU
Channel Id: undefined
Length: 8min 39sec (519 seconds)
Published: Fri Jun 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.