Instructor: Our next verb is the PUT verb.
Now, the PUT verb is quite similar to the POST
but there is one very important different.
The PUT verb is used to modify resources.
If you remember, POST is used to add resources
and PUT is used to modify them.
So, if you remember the CRUD acronym
we mentioned at the beginning of this section,
the CRUD that stands for Create, Read, Update and Delete.
Then POST is used for the create
and PUT is used for the update.
The rest of what you need to know
about the PUT verb is quite similar
to what we said about POST.
A PUT request should contain a message body
that specifies the resource to be modified.
A PUT request should not contain queries from parameters,
and you see an example here.
So, in this example you should not
include the company equals 15 parameters.
Again, because it's quite similar to the POST verb.
So, having say that, let's see a short demo
of how to implement the PUT verb.
So, here we have our Beeceptor, our loyal Beeceptor.
And again, let's click the mocking rules
and let's create a new rule.
And I guess you can guess already what's gonna happen.
We will select here PUT.
Here, we will type API and order,
And in the status, we will just modify it to Updated.
Sorry, Updated.
Great, and the status will be 200 as usual
and let's save this rule.
Now, look at this list of the APIs.
You can see that apart from the first one
that we used as a test,
all the other API have the same URL with API order.
The only different between them is the verb
This is the GET, the POST, and the PUT.
And from the REST API point of view,
these are a completely different APIs.
Okay, so now let's go to Postman,
and we will simply modify the verb here
and instead of POST, we will go to PUT and that's all.
Now, you remember that when testing the POST verb,
we've got the Status Created response.
So, now I've only changed the verb,
and let's click send and see what happens.
And we've got the updated status,
which is exactly what we wanted to get.
And, of course, the status is 200,
just as we defined in Beeceptor.
So, as you can see, PUT and POST are very similar.
And the only difference between them
is that POST is used to add resources
and the PUT is used to modify resources.
Now, let's go back to the slide
because there are some interesting topics
to discuss about PUT.
So, what do you think should happen
if I put a resource but the resource does not exist?
How can I update a non-exist resource?
What should I do in this case?
So, the specification states that in this case
a new resource should be created.
In other words, in this case
the PUT should behave just like POST.
But to be frank, you can use your own discretion.
You can also choose to return some kind
of error message stating that you cannot update
a non-exist resource.
So, again, do whatever feels right for you
in this specific context.
One of the most important definition of the PUT verb
and one that is often ignored
is that PUT should be Idempotent.
Now, if you're not familiar with this term Idempotent,
then let's explain it a bit.
What Idempotent means that
if we have some kind of action or a method,
then no matter how many times we execute this method,
the result will always be the same.
Now to understand it fully,
let's talk a little bit again about the POST.
If we are sending a POST request to create an entity,
then an entity is created.
And if we send the exact same POST to the server,
what happens?
So, in this case we will have two identical entities.
Well, probably not really identical,
probably the ID or the key of the entity will be different
but the entities themselves will be identical.
So, we can see that post is not it Idempotent
because the number of the entities that will be created
is identical to the number of the POST request I send.
However, with the PUT, the behavior is quite different
because no matter how many PUT request I send
there is always a single entity.
So, with PUT, I can be sure that the data is consistent
and no redundant entities were created using my code.
So, when you write the code that implements the PUT verb,
always remembers that PUT should be Idempotent.
And no matter how many times the request,
the PUT request is executed,
the data is always consistent
and no redundant data is created.