1
00:00:00,485 --> 00:00:01,581
- [Jose] Hi and welcome back.

2
00:00:01,581 --> 00:00:02,790
In this video, we're going to look at

3
00:00:02,790 --> 00:00:05,094
a bit more advanced Postman usage.

4
00:00:05,094 --> 00:00:09,419
It's just going to make
your life so much easier.

5
00:00:09,419 --> 00:00:11,356
Now, let's look at things that we've been

6
00:00:11,356 --> 00:00:15,183
writing over and over
again in our requests.

7
00:00:15,183 --> 00:00:19,350
An obvious one is this
http://127.0.0.1.5000/.

8
00:00:23,980 --> 00:00:26,897
This is everywhere in our requests,

9
00:00:27,915 --> 00:00:29,581
so what we're gonna do is we're gonna

10
00:00:29,581 --> 00:00:33,819
cut and leave only the / items,

11
00:00:33,819 --> 00:00:35,817
so it mimics our end point.

12
00:00:35,817 --> 00:00:37,594
This looks like the end point.

13
00:00:37,594 --> 00:00:41,761
And then what we're gonna do
is we're gonna put }.

14
00:00:44,743 --> 00:00:48,070
So, that's two opening
curly braces in front,

15
00:00:48,070 --> 00:00:51,320
url, and then two closing curly braces.

16
00:00:52,449 --> 00:00:55,424
Now, this gives us an
error because it tells us

17
00:00:55,424 --> 00:00:59,108
it cannot find that URL
because what we have to do

18
00:00:59,108 --> 00:01:03,374
is we have to go up here
to this cog and press on it

19
00:01:03,374 --> 00:01:05,855
and press Manage Environments.

20
00:01:05,855 --> 00:01:09,551
What we're gonna do is we're
gonna add a new environment.

21
00:01:09,551 --> 00:01:12,968
This environment is going to be Section 6

22
00:01:14,928 --> 00:01:17,249
and here the key is gonna be url

23
00:01:17,249 --> 00:01:20,832
and the value is going
to be this base url,

24
00:01:21,674 --> 00:01:25,999
http://127.0.0.1.5000 okay?

25
00:01:25,999 --> 00:01:30,166
So, just add that and then
close it and now we can run it

26
00:01:31,308 --> 00:01:33,458
and make sure that the
environment is selected,

27
00:01:33,458 --> 00:01:35,675
of course, up here at the top right.

28
00:01:35,675 --> 00:01:39,842
And now that is using,
essentially replacing this }

29
00:01:43,333 --> 00:01:46,416
by the url in your environment, okay?

30
00:01:49,221 --> 00:01:52,101
Alright, so what other things can we do?

31
00:01:52,101 --> 00:01:54,345
Well, the first things
first, let's go ahead

32
00:01:54,345 --> 00:01:58,512
and copy that and let's
put it in every url there.

33
00:01:59,490 --> 00:02:04,372
Make sure that you are
not replacing the /.

34
00:02:04,372 --> 00:02:08,794
The / still has to be there and
the reason I'm keeping the /

35
00:02:08,794 --> 00:02:12,627
is again, so it matches
the way the end points

36
00:02:15,744 --> 00:02:17,335
are defined in Flask.

37
00:02:17,335 --> 00:02:20,795
They're always defined as / something

38
00:02:20,795 --> 00:02:23,637
and remember to save all of these as well.

39
00:02:23,637 --> 00:02:25,470
That's also important.

40
00:02:30,570 --> 00:02:33,821
Okay, now we've got all of that done.

41
00:02:33,821 --> 00:02:36,485
There is something else
that we've been doing

42
00:02:36,485 --> 00:02:39,485
that has been a bit of a pain really

43
00:02:40,732 --> 00:02:42,982
and that is this JWT Token.

44
00:02:46,758 --> 00:02:49,708
Okay, so what we're gonna do
here is we're gonna replace

45
00:02:49,708 --> 00:02:52,533
all the string of numbers and characters

46
00:02:52,533 --> 00:02:53,616
by jwt_token.

47
00:02:55,766 --> 00:02:57,663
Okay, so we're gonna make
the authorization header

48
00:02:57,663 --> 00:02:59,413
be JWT }

49
00:03:06,330 --> 00:03:09,563
and then we're gonna save this again.

50
00:03:09,563 --> 00:03:13,590
Okay and then we're going to
look at some pretty advanced

51
00:03:13,590 --> 00:03:15,893
and pretty cool stuff here in Postman.

52
00:03:15,893 --> 00:03:19,085
We're gonna go to the off endpoint

53
00:03:19,085 --> 00:03:21,949
and then we're going to look at tests.

54
00:03:21,949 --> 00:03:22,782
Okay?

55
00:03:23,951 --> 00:03:27,034
Now, these tests are JavaScript code.

56
00:03:28,304 --> 00:03:31,904
JavaScript code that
runs after the request

57
00:03:31,904 --> 00:03:36,273
has been processed by the
server and sent back to us.

58
00:03:36,273 --> 00:03:39,987
What that means is that
when the server sends us

59
00:03:39,987 --> 00:03:43,820
the JWT Token in JSON
format, these tests here

60
00:03:44,915 --> 00:03:48,270
can access the value of the JWT Token

61
00:03:48,270 --> 00:03:52,247
and they can do things like
verify it is not empty.

62
00:03:52,247 --> 00:03:56,098
For example, they can do
things like verify it came back

63
00:03:56,098 --> 00:03:59,912
within five seconds as
opposed to 10 minutes.

64
00:03:59,912 --> 00:04:00,871
But, they can also do things

65
00:04:00,871 --> 00:04:04,511
like change an environment variable.

66
00:04:04,511 --> 00:04:06,447
So, what we're gonna do in this test

67
00:04:06,447 --> 00:04:10,949
is we're going to get the value
coming back from the server

68
00:04:10,949 --> 00:04:14,211
and we're gonna put that value
in an environment variable,

69
00:04:14,211 --> 00:04:18,378
which is going to be the
jwt_token variable there.

70
00:04:23,994 --> 00:04:27,911
So, let's go the off
endpoint, go to the tests.

71
00:04:34,524 --> 00:04:36,092
Then what we're gonna do is...

72
00:04:36,092 --> 00:04:39,092
First of all, receive the JSON code.

73
00:04:40,714 --> 00:04:43,464
So, we're gonna do the following,

74
00:04:44,470 --> 00:04:46,970
var jsondata = JSON.parse

75
00:04:51,106 --> 00:04:54,198
and then (responseBody).

76
00:04:54,198 --> 00:04:56,389
Now, what this is doing is creating

77
00:04:56,389 --> 00:04:59,082
a new variable in JavaScript.

78
00:04:59,082 --> 00:05:00,562
That's what the var does.

79
00:05:00,562 --> 00:05:03,521
It calls the variable
jsondata and what it puts

80
00:05:03,521 --> 00:05:07,685
in that variable is
the parse responseBody.

81
00:05:07,685 --> 00:05:10,616
responseBody is a variable
that's already been

82
00:05:10,616 --> 00:05:13,346
created for us by Postman
and that contains,

83
00:05:13,346 --> 00:05:17,683
essentially whatever the
server responds with.

84
00:05:17,683 --> 00:05:21,199
Now, we have to do
JSON.parse so that JavaScript

85
00:05:21,199 --> 00:05:25,381
can convert the JSON content
to a JavaScript object,

86
00:05:25,381 --> 00:05:26,978
which then gives us a bit more flexibility

87
00:05:26,978 --> 00:05:31,492
in terms of retrieving
values and things like that.

88
00:05:31,492 --> 00:05:32,483
And the way I got this code

89
00:05:32,483 --> 00:05:35,706
was by pressing one of these snippets.

90
00:05:35,706 --> 00:05:39,249
In my case, I pressed Response
body: JSON value check

91
00:05:39,249 --> 00:05:42,072
and what that does is that
gives you that code there

92
00:05:42,072 --> 00:05:46,072
and it also gives you a
test and this test says,

93
00:05:46,911 --> 00:05:51,078
tests and then in square
brackets, the name of the test

94
00:05:52,261 --> 00:05:56,114
equals and then some Boolean comparison.

95
00:05:56,114 --> 00:05:58,130
In this case, what it's checking is

96
00:05:58,130 --> 00:06:02,478
that some value in the
JSON data is equal to 100.

97
00:06:02,478 --> 00:06:06,645
But, we could do, for example,
jsonData.access_token !==""

98
00:06:14,585 --> 00:06:16,202
So, we could do something like this

99
00:06:16,202 --> 00:06:18,418
and what this does is this tells Postman

100
00:06:18,418 --> 00:06:23,326
to start checking whether the
access token is empty or not.

101
00:06:23,326 --> 00:06:27,558
Notice that in JavaScript
you need ! Followed by ==,

102
00:06:27,558 --> 00:06:31,098
whereas in Python, you just
need one = after the !.

103
00:06:31,098 --> 00:06:32,693
That's just a small difference

104
00:06:32,693 --> 00:06:34,890
between JavaScript and Python.

105
00:06:34,890 --> 00:06:36,826
Okay, so the first thing
we'll do is we'll check

106
00:06:36,826 --> 00:06:39,151
the access token is not empty.

107
00:06:39,151 --> 00:06:40,985
Then, the next thing we're
gonna do is we are going

108
00:06:40,985 --> 00:06:43,267
to go down here to this snippet, here,

109
00:06:43,267 --> 00:06:45,886
that says Set an environment variable

110
00:06:45,886 --> 00:06:48,150
and we're gonna press that
and what that's going to do

111
00:06:48,150 --> 00:06:50,560
is it's going to give us some code to set

112
00:06:50,560 --> 00:06:52,309
an environment variable.

113
00:06:52,309 --> 00:06:55,902
The variable we want to set is jwt_token,

114
00:06:55,902 --> 00:06:59,546
which is the one we
used in the get() method

115
00:06:59,546 --> 00:07:02,276
and the value is going
to be, you guessed it,

116
00:07:02,276 --> 00:07:06,443
jsonData.access_token and
we're gonna save that endpoint

117
00:07:08,644 --> 00:07:11,610
and we're gonna press send.

118
00:07:11,610 --> 00:07:13,302
I noticed another tests down here

119
00:07:13,302 --> 00:07:15,556
says one out of one passed

120
00:07:15,556 --> 00:07:19,345
because the access token was not empty.

121
00:07:19,345 --> 00:07:22,057
So, therefore this evaluated to true

122
00:07:22,057 --> 00:07:26,122
and therefore this tests
you're testing there was true.

123
00:07:26,122 --> 00:07:29,118
We can go onto the
tests and this says pass

124
00:07:29,118 --> 00:07:31,063
your test name.

125
00:07:31,063 --> 00:07:33,512
So, maybe what we can do is rename this

126
00:07:33,512 --> 00:07:36,012
to Access token was not empty.

127
00:07:38,270 --> 00:07:40,255
Okay, and then we send this again.

128
00:07:40,255 --> 00:07:41,831
Now it says that the
access token was not empty

129
00:07:41,831 --> 00:07:43,178
and that's a good thing.

130
00:07:43,178 --> 00:07:45,707
Okay, we can save that.

131
00:07:45,707 --> 00:07:47,602
Something even more awesome though,

132
00:07:47,602 --> 00:07:51,375
is that now we can go
over to this I icon there

133
00:07:51,375 --> 00:07:53,128
and notice how as well as the url,

134
00:07:53,128 --> 00:07:57,702
which we set manually,
we've not got the JWT token,

135
00:07:57,702 --> 00:07:59,908
which has been set by this here.

136
00:07:59,908 --> 00:08:02,759
If we go to the body, we can compare

137
00:08:02,759 --> 00:08:04,870
that they are the same
and indeed they are.

138
00:08:04,870 --> 00:08:07,537
Starts with eyj, ends with 0DRG.

139
00:08:08,992 --> 00:08:11,782
So, they both seem to be identical

140
00:08:11,782 --> 00:08:16,429
and now we cAn go over
to the get() endpoint

141
00:08:16,429 --> 00:08:18,846
and we can send and it works.

142
00:08:21,464 --> 00:08:23,832
Item not found because the
item doesn't exist anymore.

143
00:08:23,832 --> 00:08:26,745
But, notice that we've not
received these signature,

144
00:08:26,745 --> 00:08:30,912
invalid era because JWT
token has just been populated

145
00:08:32,722 --> 00:08:36,311
from what we got in the off endpoint.

146
00:08:36,311 --> 00:08:40,172
This does still mean that we
have to call the off method,

147
00:08:40,172 --> 00:08:43,504
the off endpoint rather,
before getting the item.

148
00:08:43,504 --> 00:08:46,417
But, we always had to do
that and now the JWT token

149
00:08:46,417 --> 00:08:50,189
gets populated for us, so we
don't have to worry about that.

150
00:08:50,189 --> 00:08:52,874
Which is pretty awesome.

151
00:08:52,874 --> 00:08:55,506
Other things that are also
interesting in the tests

152
00:08:55,506 --> 00:08:59,351
are that you can do
things like response time

153
00:08:59,351 --> 00:09:01,958
is less than 200 milliseconds.

154
00:09:01,958 --> 00:09:05,443
Normally REST API should
respond very quickly.

155
00:09:05,443 --> 00:09:08,299
So, you should make
sure that your endpoints

156
00:09:08,299 --> 00:09:10,941
respond within a
reasonable amount of time.

157
00:09:10,941 --> 00:09:13,524
200 milliseconds is fairly resonable.

158
00:09:13,524 --> 00:09:17,460
Although, you normally
expect APIs that perform well

159
00:09:17,460 --> 00:09:20,628
to respond within 50 millaseconds.

160
00:09:20,628 --> 00:09:23,330
It all depends on, sort
of, where your server is

161
00:09:23,330 --> 00:09:24,473
and things like that.

162
00:09:24,473 --> 00:09:27,468
Another interesting one is the status code

163
00:09:27,468 --> 00:09:29,301
checks that it is 200.

164
00:09:30,897 --> 00:09:32,184
So, once again, if we send this now,

165
00:09:32,184 --> 00:09:34,928
we see the tests are two out of two.

166
00:09:34,928 --> 00:09:39,095
But, if we copy these tests
and go to the postendpoint,

167
00:09:43,714 --> 00:09:48,008
and we send, we'll see that
tests are only one of two

168
00:09:48,008 --> 00:09:50,696
because the status code is not 200

169
00:09:50,696 --> 00:09:52,717
when we are creating items.

170
00:09:52,717 --> 00:09:57,454
It is 201, so we have to
remember to set these tests

171
00:09:57,454 --> 00:10:00,629
to the correct values as well.

172
00:10:00,629 --> 00:10:03,073
Okay, this was just a few advanced things

173
00:10:03,073 --> 00:10:05,337
you can do with Postman.

174
00:10:05,337 --> 00:10:08,071
You can do things, like
retrieve the values

175
00:10:08,071 --> 00:10:10,688
from the JSON response, you
can put them in environment

176
00:10:10,688 --> 00:10:13,580
variables, you can
check the content of it,

177
00:10:13,580 --> 00:10:15,798
you can check the status
code and the response times.

178
00:10:15,798 --> 00:10:17,905
Essentially, you can do anything you want

179
00:10:17,905 --> 00:10:19,714
with JavaScript in here.

180
00:10:19,714 --> 00:10:22,474
But, it's really
interesting that these tests

181
00:10:22,474 --> 00:10:27,394
can be written because
further on, along the course,

182
00:10:27,394 --> 00:10:30,328
we will be able to run
all of these endpoints

183
00:10:30,328 --> 00:10:34,245
in sequence and use them
as an automated check.

184
00:10:35,517 --> 00:10:38,791
For example, we could first
run the register endpoint,

185
00:10:38,791 --> 00:10:41,951
then the off endpoint, then create an item

186
00:10:41,951 --> 00:10:43,482
and then retrieve the item.

187
00:10:43,482 --> 00:10:46,113
That could be, what's called a collection

188
00:10:46,113 --> 00:10:50,014
and we can run that collection as a test.

189
00:10:50,014 --> 00:10:52,102
Each one of the endpoints
has it's own tests

190
00:10:52,102 --> 00:10:55,206
and if they all pass, we
know that our API is working.

191
00:10:55,206 --> 00:10:57,377
We're not going to get
into that in this video.

192
00:10:57,377 --> 00:10:58,997
But I nevertheless say
something you can do,

193
00:10:58,997 --> 00:11:02,560
and if you're interested, you
can look into that yourself,

194
00:11:02,560 --> 00:11:04,429
but we are going to look at in the future,

195
00:11:04,429 --> 00:11:07,080
inside the course as well.

196
00:11:07,080 --> 00:11:08,133
So, that's everything for this video.

197
00:11:08,133 --> 00:11:10,022
Just wanted to save you a bit of time

198
00:11:10,022 --> 00:11:12,756
and dealing with the JWT tokens and stuff.

199
00:11:12,756 --> 00:11:14,556
So, hopefully that was interesting

200
00:11:14,556 --> 00:11:16,756
and I'll see you in the very next video.

