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