1
00:00:00,788 --> 00:00:02,425
- [Narrator] Hi, and
welcome back to the course.

2
00:00:02,425 --> 00:00:04,843
In this video, we're looking
at something really cool,

3
00:00:04,843 --> 00:00:06,926
which is request parsing.

4
00:00:08,078 --> 00:00:11,654
Remember in the last video,
we looked at the PUT request

5
00:00:11,654 --> 00:00:15,189
and we determined that, if
we were creating a new item,

6
00:00:15,189 --> 00:00:17,060
we were going to use the price

7
00:00:17,060 --> 00:00:19,968
in the request's JSON payload.

8
00:00:19,968 --> 00:00:22,592
However, if we were updating the item,

9
00:00:22,592 --> 00:00:25,786
we were going to use the entire payload.

10
00:00:25,786 --> 00:00:27,304
That means that, for example,

11
00:00:27,304 --> 00:00:29,505
if the payload were to include a name,

12
00:00:29,505 --> 00:00:32,833
the item's name would change
alongside this update.

13
00:00:32,833 --> 00:00:35,705
And that may not be what we want.

14
00:00:35,705 --> 00:00:38,777
So what we're going to do
is, we're going to make sure

15
00:00:38,777 --> 00:00:42,110
that only some elements can be passed in

16
00:00:43,128 --> 00:00:45,976
through the JSON payload.

17
00:00:45,976 --> 00:00:48,953
Flask-RESTful has a really
nice way of doing this,

18
00:00:48,953 --> 00:00:50,953
which is using reqparse.

19
00:00:52,477 --> 00:00:55,454
Now, reqparse is not
part of Flask-RESTful,

20
00:00:55,454 --> 00:00:56,841
or at least the way of doing this

21
00:00:56,841 --> 00:00:58,595
is not part of Flask-RESTful.

22
00:00:58,595 --> 00:01:00,762
It was built a lot earlier

23
00:01:01,890 --> 00:01:04,262
and it was present for
command line applications

24
00:01:04,262 --> 00:01:07,443
before it was present for REST APIs.

25
00:01:07,443 --> 00:01:09,741
But it works essentially the same way,

26
00:01:09,741 --> 00:01:12,015
so once you know how to use reqparse,

27
00:01:12,015 --> 00:01:13,468
which you'll learn in this video,

28
00:01:13,468 --> 00:01:14,906
you'll be able to use it in a wide range

29
00:01:14,906 --> 00:01:16,823
of other areas as well.

30
00:01:18,018 --> 00:01:20,184
Okay, so, now that we've imported

31
00:01:20,184 --> 00:01:22,823
reqparse from Flask-RESTful,

32
00:01:22,823 --> 00:01:26,240
all we have to do is go down to the "put"

33
00:01:27,539 --> 00:01:31,522
endpoint here and what
we're going to do is say

34
00:01:31,522 --> 00:01:34,855
that parser is reqparse, request parser.

35
00:01:36,609 --> 00:01:39,044
This only initialises a new object,

36
00:01:39,044 --> 00:01:42,447
which we can use to parse the request.

37
00:01:42,447 --> 00:01:44,913
Essentially, we're going to
run the request through it

38
00:01:44,913 --> 00:01:47,879
and see what arguments match those

39
00:01:47,879 --> 00:01:50,926
that we defined in the parser.

40
00:01:50,926 --> 00:01:53,263
But, of course, we first
have to define arguments,

41
00:01:53,263 --> 00:01:56,979
so we're going to say parser.add_argument,

42
00:01:56,979 --> 00:02:00,069
and the argument's going to be price.

43
00:02:00,069 --> 00:02:04,967
And then we can also give it
a bunch of other information

44
00:02:04,967 --> 00:02:08,717
such as the type, which
is going to be float.

45
00:02:09,711 --> 00:02:11,511
Remember, we had a
problem at the beginning

46
00:02:11,511 --> 00:02:13,511
that if we passed 12.00,

47
00:02:15,304 --> 00:02:17,825
the price ended up being an
integer instead of a float.

48
00:02:17,825 --> 00:02:20,161
This should get rid of that.

49
00:02:20,161 --> 00:02:22,830
We can say required equals true

50
00:02:22,830 --> 00:02:27,511
to make sure that no request
can come through with no price.

51
00:02:27,511 --> 00:02:30,471
And finally, we can also say help equals

52
00:02:30,471 --> 00:02:33,872
"this field cannot be left blank".

53
00:02:33,872 --> 00:02:35,906
You can put any other message here.

54
00:02:35,906 --> 00:02:37,873
There's also a lot of
other different things

55
00:02:37,873 --> 00:02:39,292
that you can parse the add argument

56
00:02:39,292 --> 00:02:43,193
and we will discover them as we go along.

57
00:02:43,193 --> 00:02:44,610
Now, the parsing,

58
00:02:45,941 --> 00:02:49,431
when we actually run
this with the request,

59
00:02:49,431 --> 00:02:52,526
is going to look in the JSON payload,

60
00:02:52,526 --> 00:02:56,786
but it would also look in,
for example, form payloads,

61
00:02:56,786 --> 00:02:57,688
which we haven't looked at

62
00:02:57,688 --> 00:02:59,658
and we won't look at in this course.

63
00:02:59,658 --> 00:03:03,586
But if you have an HTML form
that sends you some data,

64
00:03:03,586 --> 00:03:05,246
you can use this request parser

65
00:03:05,246 --> 00:03:08,481
to go through the form fields as well.

66
00:03:08,481 --> 00:03:10,896
Just as a bit of trivia there.

67
00:03:10,896 --> 00:03:13,427
And finally, instead of request.get_json,

68
00:03:13,427 --> 00:03:17,033
we're going to say parser.parse_args

69
00:03:17,033 --> 00:03:18,846
and that's going to parse the arguments

70
00:03:18,846 --> 00:03:21,714
that come through the JSON payload

71
00:03:21,714 --> 00:03:25,297
and is going to put
the valid ones in data.

72
00:03:26,535 --> 00:03:29,196
Now, we've only defined price,

73
00:03:29,196 --> 00:03:33,466
so if we put any other
arguments in the JSON payload,

74
00:03:33,466 --> 00:03:37,250
they will just get erased
and we will never see them.

75
00:03:37,250 --> 00:03:38,083
Okay?

76
00:03:40,235 --> 00:03:41,622
So, that's it, really.

77
00:03:41,622 --> 00:03:42,846
I'm going to put a new line in there

78
00:03:42,846 --> 00:03:44,550
to make things a bit nicer

79
00:03:44,550 --> 00:03:47,625
and now what we've done is we have

80
00:03:47,625 --> 00:03:50,730
essentially parsed our request,

81
00:03:50,730 --> 00:03:52,446
or it will do when we run it,

82
00:03:52,446 --> 00:03:54,569
and made sure that the price element.

83
00:03:54,569 --> 00:03:56,213
the price argument is there.

84
00:03:56,213 --> 00:03:58,933
So let's go over to Postman

85
00:03:58,933 --> 00:04:02,649
and I'm going to close these things here.

86
00:04:02,649 --> 00:04:05,918
Make sure the app is running, which it is.

87
00:04:05,918 --> 00:04:08,915
And then, let's create a new item,

88
00:04:08,915 --> 00:04:11,332
which is going to be a piano.

89
00:04:12,402 --> 00:04:13,319
And I'm going to send it.

90
00:04:13,319 --> 00:04:16,481
Notice the body does have the price.

91
00:04:16,481 --> 00:04:18,764
So everything is fine.

92
00:04:18,764 --> 00:04:20,366
Now I'm going to change the body

93
00:04:20,366 --> 00:04:22,783
to be price instead of price,

94
00:04:23,681 --> 00:04:24,632
which sounds pretty much the same.

95
00:04:24,632 --> 00:04:26,071
I didn't want to say "prick" in a video,

96
00:04:26,071 --> 00:04:27,497
which I just did.

97
00:04:27,497 --> 00:04:31,664
And notice how price says "this
field cannot be left blank".

98
00:04:32,905 --> 00:04:36,858
And just so you believe me,
I'm going to change the price

99
00:04:36,858 --> 00:04:39,029
and then we're going to
go into the GET items

100
00:04:39,029 --> 00:04:41,673
and make sure that it's still $17.99.

101
00:04:41,673 --> 00:04:44,288
So the request terminated when it realised

102
00:04:44,288 --> 00:04:46,536
that the field was not there

103
00:04:46,536 --> 00:04:49,297
and that's what you would expect.

104
00:04:49,297 --> 00:04:51,369
Also, to make sure that you do believe me,

105
00:04:51,369 --> 00:04:55,387
I'm going to print(data['another'])

106
00:04:55,387 --> 00:04:58,472
which is a field inside data

107
00:04:58,472 --> 00:05:01,131
that we have not added an argument for.

108
00:05:01,131 --> 00:05:03,488
So, in other words, we've only
got an argument for price.

109
00:05:03,488 --> 00:05:05,821
We don't have an argument for another.

110
00:05:05,821 --> 00:05:07,134
And then we're going to go into Postman

111
00:05:07,134 --> 00:05:09,196
and go into the PUT method again

112
00:05:09,196 --> 00:05:11,448
and make sure the price is correct

113
00:05:11,448 --> 00:05:14,781
and I'm going to put "another" in there.

114
00:05:15,888 --> 00:05:18,924
And notice how we get a key error,

115
00:05:18,924 --> 00:05:21,660
which is the nice HTML page here.

116
00:05:21,660 --> 00:05:24,333
We get a key error "another".

117
00:05:24,333 --> 00:05:28,408
And what that means is this
thing here doesn't exist.

118
00:05:28,408 --> 00:05:31,908
Data does not have a key called "another".

119
00:05:33,671 --> 00:05:37,156
And it shouldn't, because the
only argument that we've got

120
00:05:37,156 --> 00:05:40,193
is for price, so all the other arguments,

121
00:05:40,193 --> 00:05:42,768
such as "another", just get erased

122
00:05:42,768 --> 00:05:44,561
and we will never see them.

123
00:05:44,561 --> 00:05:45,394
Okay?

124
00:05:46,342 --> 00:05:49,689
Even though "another" is
here in the JSON payload,

125
00:05:49,689 --> 00:05:52,117
we have not accepted it in the API,

126
00:05:52,117 --> 00:05:55,450
therefore it just doesn't get passed in.

127
00:05:57,276 --> 00:06:00,552
Okay, that's everything
for request parsing.

128
00:06:00,552 --> 00:06:03,214
Now I would encourage you to also do this

129
00:06:03,214 --> 00:06:05,726
in your other endpoints
where you want to make sure

130
00:06:05,726 --> 00:06:07,410
that the price is passed in

131
00:06:07,410 --> 00:06:09,392
and there's nothing else passed in.

132
00:06:09,392 --> 00:06:11,809
For example, the POST method.

133
00:06:13,204 --> 00:06:14,354
And without further ado,

134
00:06:14,354 --> 00:06:16,323
I'll see you in the very next video.

