Narrator: Our next verb is the POST verb.
Now, the POST verb is used to add resource.
If you remember, you used GET to get, or retrieve a resource
and the POST verb is used to add resource.
The POST verb should contain a message body
that specifies the resource to be added.
Or in other words,
when sending a request using the POST verb, then
sending a verb and a URL like in the GET verb is not enough.
The request should contain a body,
so that the server will know what resource it should add.
And this information, naturally, is not contained
in the URL itself because the URL points
to the resource address to be added
and not to the resource itself.
So again, a request using the POST verb should always
contain a body, specifies the resource to be added.
Now another thing to note
about the POST is that a POST request should not
contain any query string parameters.
If you remember, with GET
we can use query string parameters, but with POST we cannot.
So a URL like that is not valid for POST verb.
Okay, great.
So let's do a short demo of the POST.
So like in the previous demo, we will begin with Beeceptor.
And again let's click the Mocking Rules
and we will create a new rule.
And here, make sure to select POST in the Method.
So the URL of the API, of the POST API, we are going
to create will be /API/order.
And we will respond by saying that the status
of adding a new resource is created.
Now later in this course we'll discuss what is
the right way to return a created status.
It has something to do with the status code.
There is a specific status code Designate created
but again, we will talk about it later.
As of now, we will stay with the 200 status
and the response body that contains the status created.
Okay, great.
So now let's save the rule and go to Postman
to test the API we just created.
And again, I will drag it from my other monitor.
Here it is.
Okay, great.
So, in order to test the API we made, we are going to
first select the POST verb from this dropdown.
And now let's just enter, oh, it's right here.
Let's just enter the URL of the API.
Now, note something interesting.
Do you remember the URL that we tested
in the previous lecture with the GET verb?
So note that the URL itself, this part, is identical.
It has the same domain and the same ending.
It's API/order, but from the REST API point of view
it's a completely different API
because the verb is a different one.
So again, don't look always only on the URL,
always look also on the verb.
As we said earlier, in order to use the POST verb
we need to include a body.
So, click here on the Body tab and select row.
With row we can type the exact string
that will be sent to the server.
So naturally in this case, we want the body to
represent a new order that we want the server to add.
Let's type in a JSON representing a new order.
And we will do it like this.
(keyboard clicking) This is an order, and
the order has an order date, which is this one.
And an order user,
which we will do it like this.
And let's add also
the total cost of the order and we will put it like this.
Okay, great.
And we'll close this.
And so we have a JSON structure represent the
new order that we want to add.
So we built this body and let's click Send.
And we've got exactly the status that we expected.
So the status is created.
Of course, we are getting here the 200
which is exactly what we asked Beeceptor to return.
Size is the same as the previous demo, 218 bytes.
And it took a little bit more than one second.
Okay, great.
So this is how we are using the POST verb.
So again, the POST is used to add a resource
and in this demo we added a new order to the server.