1
00:00:00,510 --> 00:00:04,080
-: So what are the status codes that we need to be aware

2
00:00:04,080 --> 00:00:08,250
of in the success group, in the 200 group?

3
00:00:08,250 --> 00:00:11,419
So the first one, most common, most popular

4
00:00:11,419 --> 00:00:15,498
and the most well known is the 200, OK.

5
00:00:15,498 --> 00:00:18,570
This status is the default status code,

6
00:00:18,570 --> 00:00:21,648
meaning if you did not specify a status code

7
00:00:21,648 --> 00:00:23,970
this is what will be returned.

8
00:00:23,970 --> 00:00:25,944
Now there is a problem with this status

9
00:00:25,944 --> 00:00:28,664
being the default one because many developers

10
00:00:28,664 --> 00:00:31,200
aren't aware of this status code.

11
00:00:31,200 --> 00:00:34,344
They simply write their API and they don't even know

12
00:00:34,344 --> 00:00:38,580
that what gets returned to the client is the 200,

13
00:00:38,580 --> 00:00:41,760
So you should always make sure what is the exact

14
00:00:41,760 --> 00:00:44,280
status that your API returns.

15
00:00:44,280 --> 00:00:48,840
What 200 means is that the request was successful.

16
00:00:48,840 --> 00:00:52,288
No matter what the request was, but it was successful,

17
00:00:52,288 --> 00:00:56,910
and usually API clients first check for the status code,

18
00:00:56,910 --> 00:00:59,907
and if it's 200, then they know that everything is fine

19
00:00:59,907 --> 00:01:03,300
and they can go on and analyze the response body.

20
00:01:03,300 --> 00:01:05,010
If something else was returned,

21
00:01:05,010 --> 00:01:08,820
then the client will need to take some other action.

22
00:01:08,820 --> 00:01:12,630
So what is the meaning of 200 with the various verbs?

23
00:01:12,630 --> 00:01:16,950
So with GET when a get request returns 200,

24
00:01:16,950 --> 00:01:21,030
then it usually comes with the entities requested.

25
00:01:21,030 --> 00:01:24,990
So if I'm asking for a specific entity and I get 200,

26
00:01:24,990 --> 00:01:28,800
then I can assume that there is an entity

27
00:01:28,800 --> 00:01:30,570
in the body of the response.

28
00:01:30,570 --> 00:01:33,667
With POST request, usually when I get 200,

29
00:01:33,667 --> 00:01:37,500
then I also can assume the response will contain the body

30
00:01:37,500 --> 00:01:39,780
of the newly created entity.

31
00:01:39,780 --> 00:01:43,350
Remember that with POST we create entity.

32
00:01:43,350 --> 00:01:48,100
Okay, the next response code is 201, Created.

33
00:01:48,100 --> 00:01:51,960
What it means is that the request has been fulfilled

34
00:01:51,960 --> 00:01:55,050
and a new entity has been created.

35
00:01:55,050 --> 00:01:58,500
Now the response that return the 201 sometimes

36
00:01:58,500 --> 00:02:02,340
might contain a location header pointing to the entity,

37
00:02:02,340 --> 00:02:05,040
so instead of returning the actual entity

38
00:02:05,040 --> 00:02:10,039
usually a 201 response includes a pointer

39
00:02:10,050 --> 00:02:11,643
to the location of the entity.

40
00:02:12,660 --> 00:02:17,400
This status code, the 201 is the expected status code

41
00:02:17,400 --> 00:02:20,550
of POST request, which is just logical since POST

42
00:02:20,550 --> 00:02:23,103
is used to create entities.

43
00:02:24,060 --> 00:02:27,960
The next one is 202 accepted,

44
00:02:27,960 --> 00:02:31,620
what it means is that the request has been accepted

45
00:02:31,620 --> 00:02:34,320
and is pending processing.

46
00:02:34,320 --> 00:02:38,460
So if I sent the server a large chunk of entities

47
00:02:38,460 --> 00:02:41,190
to process, and the processing might take time,

48
00:02:41,190 --> 00:02:44,880
then the server can respond with 202, telling me

49
00:02:44,880 --> 00:02:47,510
that he received all the data it needs and it is

50
00:02:47,510 --> 00:02:52,510
now processing it, but the processing is still not complete.

51
00:02:53,220 --> 00:02:56,490
Now what also important to know is that no notification

52
00:02:56,490 --> 00:02:59,640
will be given when the processing is complete.

53
00:02:59,640 --> 00:03:03,210
So the server will take it's time to process the request

54
00:03:03,210 --> 00:03:05,484
and it will not send any kind of notification

55
00:03:05,484 --> 00:03:08,010
when the processing is done.

56
00:03:08,010 --> 00:03:12,390
202 status code is usually used for POST and PUT requests.

57
00:03:12,390 --> 00:03:15,525
Since these requests are a request for processing data

58
00:03:15,525 --> 00:03:18,750
and the client usually does not have to wait

59
00:03:18,750 --> 00:03:21,510
until those actions are complete.

60
00:03:21,510 --> 00:03:25,950
The next status is 204, No Content.

61
00:03:25,950 --> 00:03:29,160
What this status means is the request was fulfilled

62
00:03:29,160 --> 00:03:32,310
but no content was sent back.

63
00:03:32,310 --> 00:03:35,670
So in other words, the server did what it had to do

64
00:03:35,670 --> 00:03:40,080
but it has nothing else to say to us regarding this request.

65
00:03:40,080 --> 00:03:41,958
When responding with 204,

66
00:03:41,958 --> 00:03:46,958
the response should not include body, only the header.

67
00:03:47,100 --> 00:03:51,480
And this response is mainly used when updating large entity

68
00:03:51,480 --> 00:03:53,760
or a large collection of entities.

69
00:03:53,760 --> 00:03:56,850
So in this case, the server processed what he had to do,

70
00:03:56,850 --> 00:04:00,450
but we don't need it to send us back the entities

71
00:04:00,450 --> 00:04:04,080
that it handled because the data is quite large and we don't

72
00:04:04,080 --> 00:04:08,010
want to transfer such a large content over the network.

73
00:04:08,010 --> 00:04:12,450
Now with the 204 status code, we have a unique dilemma,

74
00:04:12,450 --> 00:04:17,370
and this dilemma is about a 204 versus the 200 status.

75
00:04:17,370 --> 00:04:19,079
So what is a dilemma?

76
00:04:19,079 --> 00:04:20,730
The dilemma goes like this,

77
00:04:20,730 --> 00:04:24,780
what should be returned when a GET request returns

78
00:04:24,780 --> 00:04:25,620
no entity.

79
00:04:25,620 --> 00:04:28,860
So say, I sent a request with query parameters

80
00:04:28,860 --> 00:04:31,200
and I was looking for a range of entities,

81
00:04:31,200 --> 00:04:34,650
and if you remember when talking about a query parameters

82
00:04:34,650 --> 00:04:38,400
we said that the number of entities returned can be anywhere

83
00:04:38,400 --> 00:04:40,560
between zero and infinity.

84
00:04:40,560 --> 00:04:44,100
So what is the correct status code when there are

85
00:04:44,100 --> 00:04:46,320
no entities to be returned?

86
00:04:46,320 --> 00:04:50,116
Should it be 200 with empty body or no body,

87
00:04:50,116 --> 00:04:54,870
or 204, which represent no content?

88
00:04:54,870 --> 00:04:56,755
So there is no agreed upon answer

89
00:04:56,755 --> 00:05:01,200
and I've seen APIs returning 200 and 204.

90
00:05:01,200 --> 00:05:05,220
My personal recommendation is to avoid the 204 and stick

91
00:05:05,220 --> 00:05:08,514
to the 200, because when sending a GET request

92
00:05:08,514 --> 00:05:11,550
that returned no entities, because there are simply

93
00:05:11,550 --> 00:05:14,670
no entities that match the parameters we sent,

94
00:05:14,670 --> 00:05:16,470
then the request was a success.

95
00:05:16,470 --> 00:05:18,847
So everything worked as expected

96
00:05:18,847 --> 00:05:23,280
and the status should be 200, which means, OK.

97
00:05:23,280 --> 00:05:27,480
So let's see a short demo of the 200 and the 201 API

98
00:05:28,560 --> 00:05:31,620
using our receptor and postman.

99
00:05:31,620 --> 00:05:33,300
So starting with receptor,

100
00:05:33,300 --> 00:05:36,540
here we are in the, in our homepage, again

101
00:05:36,540 --> 00:05:40,833
clicking the mocking rules and let's create a new rule.

102
00:05:42,000 --> 00:05:47,000
And we'll start with GET, and let's do the regular API URL.

103
00:05:47,520 --> 00:05:49,023
So we have your API,

104
00:05:49,890 --> 00:05:51,480
version one,

105
00:05:51,480 --> 00:05:53,040
order,

106
00:05:53,040 --> 00:05:57,480
and the response body is not really interesting for us,

107
00:05:57,480 --> 00:06:01,650
in this demo we will be looking mainly at the response code.

108
00:06:01,650 --> 00:06:05,850
So the default response code offered by BICEP is 200

109
00:06:05,850 --> 00:06:09,570
and we already said that 200 is usually the default

110
00:06:09,570 --> 00:06:14,160
status code, and we will leave it like this with 200.

111
00:06:14,160 --> 00:06:16,503
So let's save the rule,

112
00:06:17,640 --> 00:06:19,147
and go to Postman,

113
00:06:19,147 --> 00:06:21,780
(Mouse clicks)

114
00:06:21,780 --> 00:06:26,780
make sure we are still in GET and delete these parts.

115
00:06:27,930 --> 00:06:32,310
So we will match the URL we defined in receptor,

116
00:06:32,310 --> 00:06:34,278
and let's hit send.

117
00:06:34,278 --> 00:06:36,861
(Mouse clicks)

118
00:06:38,310 --> 00:06:40,920
And again, the body here is not really interesting

119
00:06:40,920 --> 00:06:44,520
but we want to look at this thing,

120
00:06:44,520 --> 00:06:45,600
the 200.

121
00:06:45,600 --> 00:06:50,600
So we see that as expected we received the 200 status

122
00:06:50,850 --> 00:06:53,040
which represent OK.

123
00:06:53,040 --> 00:06:56,760
Now let's take a look at the Postman console,

124
00:06:56,760 --> 00:06:57,843
which is here,

125
00:06:59,610 --> 00:07:01,593
and here, show Postman console.

126
00:07:03,171 --> 00:07:05,880
(Mouse clicking)

127
00:07:05,880 --> 00:07:09,723
Let's clear it, and let's click the send again,

128
00:07:10,740 --> 00:07:12,333
and go back to the console.

129
00:07:14,460 --> 00:07:17,730
Now let's open the request and response

130
00:07:17,730 --> 00:07:21,540
and I want you to look at the raw response.

131
00:07:21,540 --> 00:07:25,140
And you see here clearly the status code, which is one

132
00:07:25,140 --> 00:07:29,010
of the first things that gets returned from the server.

133
00:07:29,010 --> 00:07:29,843
Great.

134
00:07:29,843 --> 00:07:31,500
So this is the 200.

135
00:07:31,500 --> 00:07:36,500
Let's clear the console, and go back to receptor.

136
00:07:37,380 --> 00:07:40,140
Now let's create another rule.

137
00:07:40,140 --> 00:07:43,337
This time we are going to post the URL,

138
00:07:43,337 --> 00:07:45,570
(Mouse clicking)

139
00:07:45,570 --> 00:07:49,376
and here we are going to have a, sorry,

140
00:07:49,376 --> 00:07:50,895
API,

141
00:07:50,895 --> 00:07:52,260
V1,

142
00:07:52,260 --> 00:07:53,613
again, order,

143
00:07:55,080 --> 00:07:58,350
and now let's look at the status code.

144
00:07:58,350 --> 00:08:02,820
So for POST, we would like to see a 201, which is

145
00:08:02,820 --> 00:08:05,250
as you probably remember, Created.

146
00:08:05,250 --> 00:08:07,850
So I modify the 200 to 201,

147
00:08:07,850 --> 00:08:09,990
(mouse clicking)

148
00:08:09,990 --> 00:08:11,220
and,

149
00:08:11,220 --> 00:08:12,363
let's save it,

150
00:08:13,530 --> 00:08:15,489
and go back to Postman.

151
00:08:15,489 --> 00:08:18,210
(Mouse Clicking)

152
00:08:18,210 --> 00:08:22,590
And all we have to do now is just modify the verb

153
00:08:22,590 --> 00:08:24,963
from GET to POST.

154
00:08:26,340 --> 00:08:28,009
And let's hit send,

155
00:08:28,009 --> 00:08:30,540
(Mouse Clicks)

156
00:08:30,540 --> 00:08:33,659
and notice the status code here.

157
00:08:33,659 --> 00:08:36,456
Status code here is 201,

158
00:08:36,456 --> 00:08:37,799
which is,

159
00:08:37,799 --> 00:08:40,950
as Postman tells us, Created.

160
00:08:40,950 --> 00:08:43,950
And if you will take a look at the console,

161
00:08:43,950 --> 00:08:47,760
This is the request that was executed by Postman

162
00:08:47,760 --> 00:08:49,260
and let's open it.

163
00:08:49,260 --> 00:08:52,770
And you can see here at the raw response

164
00:08:52,770 --> 00:08:57,060
that there is the status 201, which means created.

165
00:08:57,060 --> 00:08:59,907
So this is what was returned from the server

166
00:08:59,907 --> 00:09:04,293
and this is the expected result of a POST request.

