Hated everyone they share in this video.
We're going to talk about the post request.
Now, we have seen that the data can come through the URL in a couple of formats, a regular parameter
or maybe as a query format, a couple of other ways as well.
But majority of the time, this is one way.
Another way is coming up the data from the body itself.
It can be in the form or it can be in the format of JSON.
Now Express is well known for handling both of the data with the similar kind of approach.
So you won't have to worry too much until unless the image is coming up.
The image when the image comes up, we will have a dedicated video on that and I'll explain that more
in detail right now.
Assuming that the data is only coming up as a simple format, it's usually in the JSON format that comes
in and it's much more easier to handle that.
Let's go ahead and work on with that.
So what we're going to see is as soon as you get the data in the JSON first, you have to use something
like AB use.
Now this is a middleware.
This helps you to process the data in between.
So when the data goes into the Express and is available to you in between, there is some processing
that needs to happen.
We will have some of the dedicated videos on the middleware.
In fact, we are going to write our own custom middleware, so don't you worry on that part.
So for this we have to use app use now since we will be expecting data from in the JSON format, we're
going to say that express, please take care of the things.
We are going to expect some data in the JSON.
Now, this is known as kind of a body parser.
Now in the previous days or in some between days, we have seen that body parser was not a part of the
express itself.
We need to install it separately.
So in that case we just create available and require body express and pass it on here as a middleware.
In the recent version of Express, the middleware and the body parcel comes up as default.
So all you got to do is simply say express or JSON just like that.
So it's not a big deal.
Now let's see that how this is going to work on.
Now what we're going to see is we are going to get some data into the body and we have to push the data
into the array and that's pretty much it.
And we're going to return the entirety of the message or we can just return it through that.
Yeah, it was added something like that.
So just like we have app dot get, we will have app post and yes similarly you can write, put and delete
and all of that.
So let's go ahead and work on with that.
So how you're going to make sure that the data adds up, we first have to create a route for it.
So this is going to be slash V one and slash.
Let's call this one as ADD course.
I feel free to call it as course if you want to follow those standards.
But again, we'll talk about them later on.
Request or response.
We're going to go like this.
Now, first thing is we need to access the things which are coming up.
So in this case, a good idea would be to simply go ahead and log whatever is coming up as a request
dot body.
Now, this will be a great exercise for you to understand more stuff.
I won't be doing it or let's just do it.
And after that we're going to simply say courses.
So this is my array.
I'll use the property of dot push and I'll push it as request, dot body, whatever it is coming up
in the body.
I'm just pushing it up here.
Now one more interesting thing.
Usually in the cases, we don't push it like this.
We do more refinements and more validation checks so that we know that what exactly is coming up and
it is properly validated.
Once this is done, we are going to say restart sin and we are going to just pass on a boolean, which
is going to be true that the data was added.
You can return the exact same body.
You can also return the entirety of the course object.
It's up to you how your architecture in your application.
Okay, now let's move on to the swagger and let's see that how we can add a new course to the swagger.
And we did called it up as ADD goes.
Okay.
So we're going to go ahead and copy this entirety of the stuff and let's go ahead and duplicate this.
There we go.
Nice and easy.
There we go.
Now, this one is not all of this.
This is just add codes.
Now, this is obviously not a get request.
It is a post request.
As soon as you do this as a post, you can actually go back into the browser and where is my browser?
There we go.
I can hit a reload and I can see that there should be a post request.
So there we go.
Add course is a post request.
Yes, they are color coded nicely.
You don't have to do any of that.
So this is nice.
Since we have made haven't made any other changes, this looks exactly the same.
So let's go ahead and change that.
So summary, let's go ahead and change this a little bit.
Adds a new course to existing courses that makes sense.
Now what kind of parameter you will be passing on now in this case, if the parameter is just request
or query, you can go ahead and use that.
But there are some additional additional lines that you can put up.
These are two ones, what it's going to be consumes.
So there we go.
So what we are consuming is application JSON.
And you can also optionally mention that what kind of things we produce produces.
Produces.
There we go.
Hopefully I have not done any kind of typo.
Now, usually what the output that comes up is usually in the recommended format is application JSON.
In this case we are passing on a boolean, but just showing you that, yeah, this is the most ideal
case scenario that you'll be going on.
Now notice here we have the parameters, but the issue is that in the recent format, previously it
was in the parameters if you are following the version two of it, but now it has been changed.
I want to show you directly in the documentation so it removes all the confusion.
So in here you can say that we have the request describing the request body.
Let's go ahead and reload that notice here.
Now it says all going up here.
Here we go.
It says request body.
So now, previously it was all in.
So if something is going in, in, we just say in params and in body and all of that.
Now if you're sending something in the body, you have to define it as a request body.
So make sure you confirm with the documentation one more time.
So let's go ahead and change this one.
In fact, we can change all of this.
Yeah, we don't need all of this.
So let's go ahead and write this one here.
We're going to say we are passing on some data in request body.
Make sure the B is capital in this case.
There we go.
Now what you're going to pass on the request body first.
Is it required?
Yes, this is very much required.
So let's go ahead and mark this flag as true and then is content.
So what kind of content you are passing on?
The content that we are passing on is application JSON.
You have to explicitly mention that and once this is done, all you got to do is define your schema.
And this is we have done it many times.
So schema actually we can copy paste this one because this is exactly the schema that's coming in and
there are shortcut ways of defining these schemas like this.
I'll mention that later on when we'll study the documentation a bit more.
So let's go ahead and indent this properly.
There we go.
Okay, so that's it.
The new thing that we have learned here is the request body that comes in.
Let's go ahead and save this.
Go back onto the browser here to reload on the swagger.
And there we go.
So it says could not render see the console.
So definitely there is something wrong in the request body.
Let me go ahead and quickly see that.
Got it.
It was really simple.
We can see that here in the schema.
We didn't guard the indentation.
Right.
So yes, it's a little bit of painful stuff.
But after stating a little bit, you'll find it definitely.
Let's go ahead and hit a reload and then we go.
Now it's all happy.
So we have a post request.
It says Add course's description is nice.
We have some parameters.
We have no parameters in this one.
You can additionally add your parameters to along with the request body.
No problem in that we don't have a requirement of that.
As of now.
The schema you can find that we have an example nice up here that this is kind of a schema that you
have to pass on and this is of application JSON type.
The response is also going to be JSON type and this is how you're going to get a response.
Now in this case, we are not getting a response like this.
So what we can say that this is not coming in.
Rather the type that is coming up is Boolean, save that, go back and hit reload again.
And we are getting an example, true or false value that is coming in.
So it's not really that tough told you.
Let's go ahead and try it out now.
It gives you the request body that you can pass on.
So let's go ahead and add a55.
The string is going to be a test string now since obviously it is not a persistent data, we are just
throwing it up into the array.
As soon as it hit reload or my back end reloads, it's going to just refresh and the pricing is going
to be, let's just say just nine and let's execute that.
And here we see that this is all going nicely into this URL.
The response that we got is true and we can again verify that by requesting all the arrays.
So let's go ahead and scroll back.
And that is what I love absolute about these documentation.
I can try out hit, execute and I can grab all the values.
So notice this is the latest one that we have added.
So cool, really awesome.
So now that you know how to make a post request, also how you can request some data in the request
body again reminding you it's not like you can only send request body.
Your parameters can also be a part of this entire request.
You can go ahead and add that one.
Let's go ahead and catch up in the next video.