1
00:00:00,750 --> 00:00:02,760
- [Instructor] Hi and welcome back.

2
00:00:02,760 --> 00:00:05,408
In this video we're going to
be getting a professional tool

3
00:00:05,408 --> 00:00:08,930
for API testing that's really awesome.

4
00:00:08,930 --> 00:00:11,752
It's called Postman and you can

5
00:00:11,752 --> 00:00:15,951
get it the getpostman.com/apps.

6
00:00:15,951 --> 00:00:18,403
When you get to the website, you will see

7
00:00:18,403 --> 00:00:20,439
something more or less like this.

8
00:00:20,439 --> 00:00:23,653
I see a Main button here which
is download Postman for Mac,

9
00:00:23,653 --> 00:00:25,284
but I'm assuming that if you're on Windows

10
00:00:25,284 --> 00:00:27,919
you'll see download Postman for Windows.

11
00:00:27,919 --> 00:00:30,244
So just download Postman for
your appropriate platform

12
00:00:30,244 --> 00:00:34,056
such as Windows, Linux
or Mac and instal it

13
00:00:34,056 --> 00:00:38,497
like any other normal application
and then we can run it.

14
00:00:38,497 --> 00:00:42,497
If you prefer, you can run
Postman from within Chrome

15
00:00:42,497 --> 00:00:44,970
as an add-on, you can do that as well

16
00:00:44,970 --> 00:00:47,993
if that's what you like doing instead.

17
00:00:47,993 --> 00:00:51,785
Once you've downloaded it and run it

18
00:00:51,785 --> 00:00:55,452
you will see something
that looks like this.

19
00:00:59,753 --> 00:01:01,455
It'll be slightly different
because I've already

20
00:01:01,455 --> 00:01:04,213
got some stuff here, but what you'll see

21
00:01:04,213 --> 00:01:07,358
is a window that looks
more or less like this.

22
00:01:07,358 --> 00:01:09,324
So you'll have your runner, import

23
00:01:09,324 --> 00:01:10,283
and a few buttons up here.

24
00:01:10,283 --> 00:01:12,840
You'll have a team library
which will not be accessible,

25
00:01:12,840 --> 00:01:16,514
and also you'll have your stuff up here.

26
00:01:16,514 --> 00:01:18,911
You'll have to sign up to get here first,

27
00:01:18,911 --> 00:01:23,541
but that's sort of a given with
any application these days.

28
00:01:23,541 --> 00:01:25,664
Then what I want you to do is create

29
00:01:25,664 --> 00:01:30,365
a new folder here, and I've
called it REST API course,

30
00:01:30,365 --> 00:01:32,053
but you can call it whatever you want.

31
00:01:32,053 --> 00:01:36,220
Your collection, give it a
nice name, it's usually better.

32
00:01:38,509 --> 00:01:43,103
Okay, and then in here,
you can create folders

33
00:01:43,103 --> 00:01:45,415
I created a folder called section three

34
00:01:45,415 --> 00:01:47,066
because this is the section we're in.

35
00:01:47,066 --> 00:01:48,814
I would recommend you do that as well.

36
00:01:48,814 --> 00:01:50,244
It'll be very useful later on for you

37
00:01:50,244 --> 00:01:53,975
to have these collections
to bring your memory back

38
00:01:53,975 --> 00:01:55,725
if you forget things.

39
00:01:56,732 --> 00:02:01,417
Then we're going to go over
to the main sort of content.

40
00:02:01,417 --> 00:02:05,841
This API testing tool
let's us make requests

41
00:02:05,841 --> 00:02:09,628
and as you can see the
first button here is get.

42
00:02:09,628 --> 00:02:11,816
We can change that and we can use any

43
00:02:11,816 --> 00:02:15,566
of the other available
HTTP verbs, GET, POST,

44
00:02:16,470 --> 00:02:20,097
but also, PUT, PATCH,
DELETE, COPY, HEAD, OPTIONS,

45
00:02:20,097 --> 00:02:21,264
and many more.

46
00:02:23,318 --> 00:02:26,049
In this API we've only used GET and POST,

47
00:02:26,049 --> 00:02:29,322
we will use others as
the course progresses.

48
00:02:29,322 --> 00:02:31,454
Then we have to enter the request URL,

49
00:02:31,454 --> 00:02:35,621
so that's gonna be
HTTP://107.0.0.1:5000/store.

50
00:02:42,226 --> 00:02:44,614
Once again, this is your local computer

51
00:02:44,614 --> 00:02:46,299
this is the port that you're running on

52
00:02:46,299 --> 00:02:49,970
and this is the endpoint we're accessing.

53
00:02:49,970 --> 00:02:52,416
Then you've got a blue button called Send.

54
00:02:52,416 --> 00:02:54,135
Don't know why I said green there.

55
00:02:54,135 --> 00:02:56,683
A blue button called Send
and when you press that

56
00:02:56,683 --> 00:02:59,665
that will execute the request,
do make sure, however,

57
00:02:59,665 --> 00:03:02,924
to have your server running,
so I'm gonna make sure

58
00:03:02,924 --> 00:03:06,603
that it is running,
that's the latest version

59
00:03:06,603 --> 00:03:09,054
and then you can press Send.

60
00:03:09,054 --> 00:03:13,371
And what you get back is your API content.

61
00:03:13,371 --> 00:03:16,341
So exactly as we would expect.

62
00:03:16,341 --> 00:03:18,832
Once that's done you can also press Save

63
00:03:18,832 --> 00:03:21,853
to remember this request,
normally I save my requests

64
00:03:21,853 --> 00:03:25,078
just as the endpoint name, and save it

65
00:03:25,078 --> 00:03:28,587
to the appropriate
collection and press save.

66
00:03:28,587 --> 00:03:31,060
Now you've got it here, and you can easily

67
00:03:31,060 --> 00:03:33,470
go back to it at any point.

68
00:03:33,470 --> 00:03:35,734
Now what I would recommend you do now

69
00:03:35,734 --> 00:03:39,534
is to press the three
buttons here, the three dots,

70
00:03:39,534 --> 00:03:42,277
and press Duplicate, then make sure

71
00:03:42,277 --> 00:03:45,044
to go into that endpoint.

72
00:03:45,044 --> 00:03:47,533
If you then go and change this
one and then you press save,

73
00:03:47,533 --> 00:03:49,098
it will be over written and you will lose

74
00:03:49,098 --> 00:03:50,909
the other endpoint, so just make sure

75
00:03:50,909 --> 00:03:55,421
to keep duplicating these
endpoints as you go along.

76
00:03:55,421 --> 00:03:59,588
This endpoint we're going to
call /store/My Wonderful Store.

77
00:04:01,145 --> 00:04:04,403
Which, as you remember, is what we've got

78
00:04:04,403 --> 00:04:07,570
in our stores list as our first store.

79
00:04:09,318 --> 00:04:10,985
So let's press Send.

80
00:04:12,153 --> 00:04:14,266
And as you can see we get back the data

81
00:04:14,266 --> 00:04:17,146
that we would expect from this store.

82
00:04:17,146 --> 00:04:20,463
So that's really great, we
can see that it is working.

83
00:04:20,463 --> 00:04:22,788
Once again, press Save to make sure

84
00:04:22,788 --> 00:04:26,488
that this endpoint is
saved and then you can,

85
00:04:26,488 --> 00:04:28,348
once again. press the three buttons,

86
00:04:28,348 --> 00:04:30,398
the three dots, and press Edit

87
00:04:30,398 --> 00:04:34,565
and give this a nicer
name, such as /store/name.

88
00:04:35,442 --> 00:04:37,925
Then go ahead and duplicate that.

89
00:04:37,925 --> 00:04:40,583
Go over to the new store and for example,

90
00:04:40,583 --> 00:04:43,083
we can access item, we can see

91
00:04:44,230 --> 00:04:47,730
that we get the items for this store back.

92
00:04:48,812 --> 00:04:51,886
Once again press Save, edit this one,

93
00:04:51,886 --> 00:04:55,227
give it an appropriate
name and that's that.

94
00:04:55,227 --> 00:04:58,727
Now we have created our three GET requests

95
00:04:59,636 --> 00:05:02,052
and these are not really the problem.

96
00:05:02,052 --> 00:05:04,324
These are just really accessing a URL

97
00:05:04,324 --> 00:05:05,956
and then we get some data back.

98
00:05:05,956 --> 00:05:10,077
Now, the more interesting bit
is when it comes to creating.

99
00:05:10,077 --> 00:05:14,244
So let's duplicate the GET
store, go over to that endpoint

100
00:05:16,275 --> 00:05:18,525
and change the GET to POST.

101
00:05:20,815 --> 00:05:25,063
Now when we run this,
we will get an error.

102
00:05:25,063 --> 00:05:28,986
As you can see this is
an internal server error

103
00:05:28,986 --> 00:05:33,208
because we have not sent it
the data that it expects.

104
00:05:33,208 --> 00:05:36,988
We have to send it some JSON
data, and using JavaScript

105
00:05:36,988 --> 00:05:39,117
that's slightly more
complicated and out of

106
00:05:39,117 --> 00:05:43,224
the scope of this course but
using Postman it's really easy.

107
00:05:43,224 --> 00:05:46,016
Remember to save your endpoint first.

108
00:05:46,016 --> 00:05:49,551
Change the name to it's more reasonable.

109
00:05:49,551 --> 00:05:54,435
Let's go over to the headers
and a header of a request

110
00:05:54,435 --> 00:05:58,720
is the first thing that
gets analysed by the server.

111
00:05:58,720 --> 00:06:00,791
So the first thing that
flask is going to do

112
00:06:00,791 --> 00:06:02,531
when it receives a
request, it is gonna look

113
00:06:02,531 --> 00:06:04,381
at the headers to try and understand

114
00:06:04,381 --> 00:06:07,249
sort of what this request is.

115
00:06:07,249 --> 00:06:08,717
In the headers we can say things like

116
00:06:08,717 --> 00:06:12,564
what sort of data, what
type of data we're sending.

117
00:06:12,564 --> 00:06:17,385
So this is a set of key value
pairs and the first header

118
00:06:17,385 --> 00:06:21,454
is going to be Content-Type and the value

119
00:06:21,454 --> 00:06:25,037
is going to be application/json.

120
00:06:26,525 --> 00:06:29,846
Now, when we send a
request with this header

121
00:06:29,846 --> 00:06:33,053
the server is going to
be expecting JSON code

122
00:06:33,053 --> 00:06:34,553
to be in the body.

123
00:06:35,692 --> 00:06:37,541
So we're going to go to the body

124
00:06:37,541 --> 00:06:40,864
and we're going to select raw.

125
00:06:40,864 --> 00:06:43,651
Notice how because we've got a
header, Postman automatically

126
00:06:43,651 --> 00:06:46,818
selects JSON from this drop-down list.

127
00:06:48,065 --> 00:06:52,029
But there are many other
types of data we can send.

128
00:06:52,029 --> 00:06:53,713
We're also going to look in the future

129
00:06:53,713 --> 00:06:57,880
at what form data and what
x-www-form-urlencoded are.

130
00:06:59,433 --> 00:07:02,964
But, don't worry about those for now.

131
00:07:02,964 --> 00:07:05,872
In the body, because
we're creating a store

132
00:07:05,872 --> 00:07:08,084
the only thing we need is the store name.

133
00:07:08,084 --> 00:07:12,790
So remember, JSON always
uses double quotation marks,

134
00:07:12,790 --> 00:07:15,540
so that's "name": "Another store"

135
00:07:16,561 --> 00:07:19,314
and this is all the data
we need in our body.

136
00:07:19,314 --> 00:07:22,314
So let's see of this and press Send.

137
00:07:24,269 --> 00:07:25,701
And there you can see we get the

138
00:07:25,701 --> 00:07:28,664
new store returned as you would expect.

139
00:07:28,664 --> 00:07:31,148
Let's go and look at our code.

140
00:07:31,148 --> 00:07:33,659
And the new store dictionary returns

141
00:07:33,659 --> 00:07:37,826
a new store with a new name
and an empty items list.

142
00:07:39,679 --> 00:07:42,430
We append that to the stores and then

143
00:07:42,430 --> 00:07:44,590
we return the new store.

144
00:07:44,590 --> 00:07:46,870
And that's indeed what we've got here.

145
00:07:46,870 --> 00:07:50,164
Remember to save the request
then go to your GET store

146
00:07:50,164 --> 00:07:53,289
and when you press Send
should we be expecting

147
00:07:53,289 --> 00:07:55,456
the new store to come out?

148
00:07:58,047 --> 00:08:01,703
Yes we should, and indeed
we have here our stores list

149
00:08:01,703 --> 00:08:05,870
and there have the first
store and the second store.

150
00:08:07,021 --> 00:08:09,993
Now let's go over to our last endpoint.

151
00:08:09,993 --> 00:08:12,743
Duplicate the GET store name item

152
00:08:14,457 --> 00:08:17,428
and I'm going to change it to a post

153
00:08:17,428 --> 00:08:20,178
I'm gonna save it, edit the name,

154
00:08:22,551 --> 00:08:24,243
and then we are once again

155
00:08:24,243 --> 00:08:25,172
gonna do the same thing.

156
00:08:25,172 --> 00:08:28,831
So what's the first thing we have to do?

157
00:08:28,831 --> 00:08:30,546
That's right, set the header

158
00:08:30,546 --> 00:08:34,379
to be content-type application/json

159
00:08:38,239 --> 00:08:40,853
and then the body to be raw.

160
00:08:40,853 --> 00:08:43,585
And here we're going to
set the name of the item

161
00:08:43,585 --> 00:08:46,502
to be another item, price, and this

162
00:08:48,508 --> 00:08:51,758
is going to be a decimal so just 10.99.

163
00:08:52,692 --> 00:08:57,401
Save and Send, and we get
an internal server error

164
00:08:57,401 --> 00:09:00,734
so we've evidently done something wrong.

165
00:09:01,935 --> 00:09:05,834
So what to do now is go
over to the terminal,

166
00:09:05,834 --> 00:09:09,751
look at the errors and
we've made a small error

167
00:09:10,719 --> 00:09:13,461
somewhere in the pass so
this is the trace back,

168
00:09:13,461 --> 00:09:16,347
it's a lot longer than it used to be.

169
00:09:16,347 --> 00:09:19,026
At the very bottom, we see the error name.

170
00:09:19,026 --> 00:09:23,489
NameError: is the name,
'request data' is not defined.

171
00:09:23,489 --> 00:09:25,283
Directly above the error, we see

172
00:09:25,283 --> 00:09:28,310
the line of our code that went wrong.

173
00:09:28,310 --> 00:09:31,623
This is the file app.py line 51

174
00:09:31,623 --> 00:09:35,790
and we see request_data
equal equal request.get_json.

175
00:09:38,279 --> 00:09:40,612
Do you spot the error there?

176
00:09:44,603 --> 00:09:47,538
It's easy to make small
typos while recording

177
00:09:47,538 --> 00:09:49,203
but nevertheless is gonna be very useful

178
00:09:49,203 --> 00:09:53,707
the problem is that equal
equal is used for comparison

179
00:09:53,707 --> 00:09:57,874
such as here, whereas a single
equal is used for assignment.

180
00:09:59,982 --> 00:10:01,741
So that was the small error there,

181
00:10:01,741 --> 00:10:04,537
make sure to restart your server,

182
00:10:04,537 --> 00:10:06,787
and then run Postman again.

183
00:10:08,916 --> 00:10:12,327
And there we get our other item.

184
00:10:12,327 --> 00:10:15,564
Something to remember though,
we've restarted the server

185
00:10:15,564 --> 00:10:18,264
so when we get our list of stores,

186
00:10:18,264 --> 00:10:20,710
what do you think is gonna come out?

187
00:10:20,710 --> 00:10:23,043
Our two stores or one store?

188
00:10:27,589 --> 00:10:30,072
And this is the problem,
we're only gonna get

189
00:10:30,072 --> 00:10:33,880
one store back because
whenever we restart the server

190
00:10:33,880 --> 00:10:36,536
we lose the applications memory,

191
00:10:36,536 --> 00:10:39,829
and therefore the stores
variable gets deleted.

192
00:10:39,829 --> 00:10:42,291
When we start the server again,

193
00:10:42,291 --> 00:10:45,643
the stores variable gets
set to this list again,

194
00:10:45,643 --> 00:10:48,271
which only has one store.

195
00:10:48,271 --> 00:10:50,057
Obviously the solution to that is

196
00:10:50,057 --> 00:10:54,261
to store things permanently
in a database or in a file.

197
00:10:54,261 --> 00:10:56,650
Over the next few sections,
we are going to look

198
00:10:56,650 --> 00:10:59,176
into how to store things into a database.

199
00:10:59,176 --> 00:11:02,890
That seems fairly useful for a REST API

200
00:11:02,890 --> 00:11:05,150
to be able to do, so we're going to look

201
00:11:05,150 --> 00:11:08,434
into SQLAlchemy to easily
and efficiently store

202
00:11:08,434 --> 00:11:10,864
our resources such as our stores

203
00:11:10,864 --> 00:11:13,197
and our items to a database.

204
00:11:14,091 --> 00:11:15,282
So this is Postman.

205
00:11:15,282 --> 00:11:19,848
Postman is extremely
useful for testing API's

206
00:11:19,848 --> 00:11:23,738
I cannot stress that enough,
I use this thing everyday

207
00:11:23,738 --> 00:11:27,238
at work and I know most people do as well.

208
00:11:28,323 --> 00:11:30,096
It's because it just let's you have

209
00:11:30,096 --> 00:11:32,557
all your API endpoint's
here and whenever you

210
00:11:32,557 --> 00:11:34,733
make a change to one of
them, or whenever you want

211
00:11:34,733 --> 00:11:37,836
to verify that something
works the way you expect,

212
00:11:37,836 --> 00:11:41,376
you just go in here and run the endpoint.

213
00:11:41,376 --> 00:11:43,777
Then you can check that the
data returned is correct,

214
00:11:43,777 --> 00:11:47,580
the thing is coming back as
opposed to giving you an error

215
00:11:47,580 --> 00:11:50,572
and things like that,
so it's really essential

216
00:11:50,572 --> 00:11:52,676
that whenever you're creating REST API's

217
00:11:52,676 --> 00:11:56,083
you keep all your endpoints here so you

218
00:11:56,083 --> 00:11:58,926
can test them as you go along.

219
00:11:58,926 --> 00:12:01,147
That's everything for this video.

220
00:12:01,147 --> 00:12:02,631
Hopefully you learned something about

221
00:12:02,631 --> 00:12:04,982
how software developers
work professionally

222
00:12:04,982 --> 00:12:08,338
with REST API's and I'll see
you in the very next one.

