-: So what are the status codes that we need to be aware
of in the success group, in the 200 group?
So the first one, most common, most popular
and the most well known is the 200, OK.
This status is the default status code,
meaning if you did not specify a status code
this is what will be returned.
Now there is a problem with this status
being the default one because many developers
aren't aware of this status code.
They simply write their API and they don't even know
that what gets returned to the client is the 200,
So you should always make sure what is the exact
status that your API returns.
What 200 means is that the request was successful.
No matter what the request was, but it was successful,
and usually API clients first check for the status code,
and if it's 200, then they know that everything is fine
and they can go on and analyze the response body.
If something else was returned,
then the client will need to take some other action.
So what is the meaning of 200 with the various verbs?
So with GET when a get request returns 200,
then it usually comes with the entities requested.
So if I'm asking for a specific entity and I get 200,
then I can assume that there is an entity
in the body of the response.
With POST request, usually when I get 200,
then I also can assume the response will contain the body
of the newly created entity.
Remember that with POST we create entity.
Okay, the next response code is 201, Created.
What it means is that the request has been fulfilled
and a new entity has been created.
Now the response that return the 201 sometimes
might contain a location header pointing to the entity,
so instead of returning the actual entity
usually a 201 response includes a pointer
to the location of the entity.
This status code, the 201 is the expected status code
of POST request, which is just logical since POST
is used to create entities.
The next one is 202 accepted,
what it means is that the request has been accepted
and is pending processing.
So if I sent the server a large chunk of entities
to process, and the processing might take time,
then the server can respond with 202, telling me
that he received all the data it needs and it is
now processing it, but the processing is still not complete.
Now what also important to know is that no notification
will be given when the processing is complete.
So the server will take it's time to process the request
and it will not send any kind of notification
when the processing is done.
202 status code is usually used for POST and PUT requests.
Since these requests are a request for processing data
and the client usually does not have to wait
until those actions are complete.
The next status is 204, No Content.
What this status means is the request was fulfilled
but no content was sent back.
So in other words, the server did what it had to do
but it has nothing else to say to us regarding this request.
When responding with 204,
the response should not include body, only the header.
And this response is mainly used when updating large entity
or a large collection of entities.
So in this case, the server processed what he had to do,
but we don't need it to send us back the entities
that it handled because the data is quite large and we don't
want to transfer such a large content over the network.
Now with the 204 status code, we have a unique dilemma,
and this dilemma is about a 204 versus the 200 status.
So what is a dilemma?
The dilemma goes like this,
what should be returned when a GET request returns
no entity.
So say, I sent a request with query parameters
and I was looking for a range of entities,
and if you remember when talking about a query parameters
we said that the number of entities returned can be anywhere
between zero and infinity.
So what is the correct status code when there are
no entities to be returned?
Should it be 200 with empty body or no body,
or 204, which represent no content?
So there is no agreed upon answer
and I've seen APIs returning 200 and 204.
My personal recommendation is to avoid the 204 and stick
to the 200, because when sending a GET request
that returned no entities, because there are simply
no entities that match the parameters we sent,
then the request was a success.
So everything worked as expected
and the status should be 200, which means, OK.
So let's see a short demo of the 200 and the 201 API
using our receptor and postman.
So starting with receptor,
here we are in the, in our homepage, again
clicking the mocking rules and let's create a new rule.
And we'll start with GET, and let's do the regular API URL.
So we have your API,
version one,
order,
and the response body is not really interesting for us,
in this demo we will be looking mainly at the response code.
So the default response code offered by BICEP is 200
and we already said that 200 is usually the default
status code, and we will leave it like this with 200.
So let's save the rule,
and go to Postman,
(Mouse clicks)
make sure we are still in GET and delete these parts.
So we will match the URL we defined in receptor,
and let's hit send.
(Mouse clicks)
And again, the body here is not really interesting
but we want to look at this thing,
the 200.
So we see that as expected we received the 200 status
which represent OK.
Now let's take a look at the Postman console,
which is here,
and here, show Postman console.
(Mouse clicking)
Let's clear it, and let's click the send again,
and go back to the console.
Now let's open the request and response
and I want you to look at the raw response.
And you see here clearly the status code, which is one
of the first things that gets returned from the server.
Great.
So this is the 200.
Let's clear the console, and go back to receptor.
Now let's create another rule.
This time we are going to post the URL,
(Mouse clicking)
and here we are going to have a, sorry,
API,
V1,
again, order,
and now let's look at the status code.
So for POST, we would like to see a 201, which is
as you probably remember, Created.
So I modify the 200 to 201,
(mouse clicking)
and,
let's save it,
and go back to Postman.
(Mouse Clicking)
And all we have to do now is just modify the verb
from GET to POST.
And let's hit send,
(Mouse Clicks)
and notice the status code here.
Status code here is 201,
which is,
as Postman tells us, Created.
And if you will take a look at the console,
This is the request that was executed by Postman
and let's open it.
And you can see here at the raw response
that there is the status 201, which means created.
So this is what was returned from the server
and this is the expected result of a POST request.