Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
With resources defined and everything connected with Blueprints, it's time to handle incoming arguments. Flask-RESTful provides a solid tool known as Reqparse for specifying and validating submitted data.
- Postman
- [Documentation for
reqparse
]https://flask-restplus.readthedocs.io/en/stable/parsing.html) - Locations for
reqparse
arguments
-
0:00
Our resources are ready for action, but they don't yet
-
0:03
know how to take in input from our users.
-
0:05
We need some way of telling them what pieces of data they should expect,
-
0:08
like a title and URL for the courses, and also what those pieces of data should be.
-
0:13
Title should be strings, for example.
-
0:16
If we were doing this with models and
-
0:17
views, we'd use a form or some other sort of validation library.
-
0:21
Flask restful actually provides a validation library for
-
0:24
us that works really well, it's called reqparse, and it's for
-
0:27
parsing requests, let's go see how to use it.
-
0:31
So once I get reqparse set up,
-
0:33
I'm gonna be using Postman to test the API that I'm building.
-
0:37
This is a good time for you to go and get it installed though, there is both
-
0:40
a Chrome app and an OS X app, both are free and work more or less the same way.
-
0:45
I'll put a link to their site in the teachers notes.
-
0:48
I'm going to go ahead and put in the Chrome app though, because,
-
0:51
you know, I'm running Chrome.
-
0:52
And the cool thing is,
-
0:56
it pops up here in your little bar and then you hit Postman and there's Postman,
-
1:02
so cool, I'm going to skip that go straight to the app.
-
1:06
Reqparse tells the API how to parse incoming requests.
-
1:10
If you've used the Python module arg pars it's pretty similar so
-
1:15
what I wanna do this over here and courses.py, I need to import reqparse.
-
1:25
And then, I'm gonna add a parser to the course list thing here
-
1:29
first of the course list resource.
-
1:31
I don't know why I call it a thing, it's just a thing today.
-
1:34
All right, so, inside init, we're gonna do self.reqparse
-
1:40
= reqparse.RequestParser,
-
1:45
sometimes you just can't predict the variable names.
-
1:49
And so self.reqparse.add_argument, so we add arguments to
-
1:55
the parser for each of the things that we want to have come in each of the fields.
-
2:01
So for courses, I want to have one for title, I wanna have one for URL.
-
2:05
So let's add title first.
-
2:09
It is true, it is required, you have to have it.
-
2:12
If you don't give it to me, I'm going to say, no course title provided.
-
2:16
I could just say, no title provided, but either way.
-
2:20
And then location is actually going to be a list, it's going to be form and json.
-
2:26
So what this says is that, I need to tell reqparse where to look for this data.
-
2:34
So, I'm telling it to look in forming coded data or
-
2:37
to look in json data, which is in the body of the request.
-
2:41
Whichever one comes last is the one that's looked at first,
-
2:45
it's a little weird, but that's the way that one works.
-
2:49
All right so let's add the other argument here, self.reqparse.add_argument,
-
2:54
you can probably guess how most of this goes.
-
2:57
It's going to be url, required=true,
-
3:02
help=No course URL provided,
-
3:07
and location is gonna equal form, and json.
-
3:11
There are lots of other locations, I will link
-
3:14
to that documentation in the teacher's notes, so you can check that out.
-
3:18
Everything here is fairly straightforward, this is very similar to a forms library,
-
3:22
where you're kinda specifying what all this stuff is.
-
3:26
And by the way, if you're familiar with REST you might be thinking form here is
-
3:32
form data and it's not it's the X dash dab dab dash form URL link encoded
-
3:38
header which is like the standard one that HTML forms send.
-
3:43
So the basically the idea here is you can send a form to your API and
-
3:46
it'll work just like you were sending an XHR request or something like that.
-
3:50
And then at the end here, I do want to go ahead and
-
3:56
call super, and call init on that.
-
4:00
All right, so there we go.
-
4:04
This just makes sure that the standard set up goes ahead and it happens.
-
4:08
Let's look over here at Postman, now the cool thing is you can you see this
-
4:12
history thing you can save your post and stuff, which is kinda neat.
-
4:16
So, let's come over here and
-
4:20
get the URL, which is that one, all right.
-
4:25
If I do a get to courses and send, then I get back Json.
-
4:33
That's cool, that's exactly what we expected to get.
-
4:35
Okay, so let's try and do just a blank post to
-
4:41
it and I get back method is not allowed for this requested URL.
-
4:46
You know what I forgot all about that, I didn't add a post, did I?
-
4:49
All right let's go, we've got to get, let's add a post.
-
4:53
All right it's a def post(self), all right, and
-
4:58
then let's actually break apart these arguments.
-
5:02
So args=self.reqparse .parse_args,
-
5:06
I dare you to say that line like five times fast.
-
5:11
And we're gonna return jsonify, you know what, we're gonna return.
-
5:17
We're gonna return nothing.
-
5:20
Okay, so we get the same response back on get or post but
-
5:23
on post it parses these args.
-
5:25
All right, so now,
-
5:27
I should be able to post it because the method's allowed now, I should have it.
-
5:31
So let's hit send and no course title provided, that's right,
-
5:37
I didn't provide a course title.
-
5:39
And if you look here, you can see the status is a 400 bad request.
-
5:43
So good, that's what i should get because it is a bad request,
-
5:46
it doesn't have all of the right data.
-
5:49
All right so let's come over to body and let's say raw and
-
5:54
then we're going to put in title is Python Collections.
-
5:59
Okay, so we're just gonna send that, that's all we're gonna send.
-
6:04
Can I change, yeah.
-
6:06
So JSON application JSON, okay, so send.
-
6:11
And what did I get here, I got No course URL provided,
-
6:17
which is also true, I did not provide a URL.
-
6:20
All right, now, let's say URL and
-
6:23
i'm just gonna send python-collections because I'm just playing with this,
-
6:29
I don't know what I'm doing.
-
6:30
Let's send that, and I get back the data.
-
6:36
Wow, okay, so I got back a 200,
-
6:41
everything's okay, and I got back the data.
-
6:43
This isn't what I wanted to get though, because python-collections,
-
6:47
right here, that's not a valid URL.
-
6:50
So we need to make the parsers smarter.
-
6:52
Okay flask REST will come with a bunch of predefined inputs,
-
6:56
you can make your own but you often probably don't have to though.
-
7:00
The one that I need is included, so I'm going to use it.
-
7:03
So I'm gonna come up here to reqparse and I'm also import inputs,
-
7:09
going to have a lot of imports here from restful.
-
7:13
So, now title here by default these are always strings so
-
7:16
I don't need to add anything there.
-
7:18
So here on add argument, I'm going to add a new argument to the argument and I'm
-
7:23
gonna be type and I am going to put the type is a URL and it comes from inputs.
-
7:30
So now let's see what happens when I try to post a bad URL.
-
7:35
So I hit send, and now I've got the 400, and the no course URL provided.
-
7:40
Great, because that's true, I did not send a URL.
-
7:45
All right, so now what if I send an actual valid URL?
-
7:48
Let's come back over here https://teamtreehouse.com/library/python--
-
7:55
collections.
-
7:56
All right, so send that.
-
7:58
I have 200 and I got back my courses list.
-
8:02
So cool, that's the 200 that I expected.
-
8:05
I want to add one more part, I wanna actually save the model instance.
-
8:08
I'm not going to about changing the output though.
-
8:11
Because that's just a lot of trouble for right now and
-
8:13
we'll deal with that in a minute.
-
8:15
So I've got models imported already, so that's cool.
-
8:18
So then down here once I parse the args,
-
8:21
the args basically become a dictionary of all these items.
-
8:25
So I'm gonna do is I'm gonna do models.Course.create(**args).
-
8:31
And that will take the args dictionary and feed all of that data into Course.create.
-
8:38
So, cool, and now, if I go and submit my thing again,
-
8:44
still got a 200 and I don't have a way to look at this yet.
-
8:49
We'll check out the data that's in the database, in the next video.
-
8:53
For now though,
-
8:53
I'm gonna add the rest of these request parsers, while you do a code challenge.
-
8:57
You should try and do the requests parsers yourself too, to both course,
-
9:01
singular, and to review list and review.
-
9:04
And in the next video you can compare yours to mine.
-
9:06
Reqparse handles the request part of the request response cycle,
-
9:11
we still need to deal with the response half.
-
9:13
We'll do that in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up