WEBVTT

00:03.730 --> 00:06.100
Hated everyone they share in this video.

00:06.100 --> 00:08.430
We're going to talk about the post request.

00:08.440 --> 00:13.600
Now, we have seen that the data can come through the URL in a couple of formats, a regular parameter

00:13.600 --> 00:16.810
or maybe as a query format, a couple of other ways as well.

00:16.810 --> 00:19.260
But majority of the time, this is one way.

00:19.270 --> 00:22.900
Another way is coming up the data from the body itself.

00:22.900 --> 00:26.080
It can be in the form or it can be in the format of JSON.

00:26.080 --> 00:30.790
Now Express is well known for handling both of the data with the similar kind of approach.

00:30.790 --> 00:34.270
So you won't have to worry too much until unless the image is coming up.

00:34.300 --> 00:39.010
The image when the image comes up, we will have a dedicated video on that and I'll explain that more

00:39.010 --> 00:40.300
in detail right now.

00:40.300 --> 00:45.550
Assuming that the data is only coming up as a simple format, it's usually in the JSON format that comes

00:45.550 --> 00:48.010
in and it's much more easier to handle that.

00:48.040 --> 00:49.660
Let's go ahead and work on with that.

00:49.660 --> 00:54.790
So what we're going to see is as soon as you get the data in the JSON first, you have to use something

00:54.790 --> 00:56.050
like AB use.

00:56.050 --> 00:57.400
Now this is a middleware.

00:57.400 --> 00:59.950
This helps you to process the data in between.

00:59.950 --> 01:05.170
So when the data goes into the Express and is available to you in between, there is some processing

01:05.170 --> 01:06.310
that needs to happen.

01:06.310 --> 01:09.490
We will have some of the dedicated videos on the middleware.

01:09.490 --> 01:13.450
In fact, we are going to write our own custom middleware, so don't you worry on that part.

01:13.450 --> 01:19.750
So for this we have to use app use now since we will be expecting data from in the JSON format, we're

01:19.750 --> 01:22.720
going to say that express, please take care of the things.

01:22.720 --> 01:25.240
We are going to expect some data in the JSON.

01:25.450 --> 01:28.300
Now, this is known as kind of a body parser.

01:28.330 --> 01:34.420
Now in the previous days or in some between days, we have seen that body parser was not a part of the

01:34.420 --> 01:35.200
express itself.

01:35.200 --> 01:36.880
We need to install it separately.

01:36.880 --> 01:42.460
So in that case we just create available and require body express and pass it on here as a middleware.

01:42.460 --> 01:47.290
In the recent version of Express, the middleware and the body parcel comes up as default.

01:47.290 --> 01:50.800
So all you got to do is simply say express or JSON just like that.

01:50.800 --> 01:52.270
So it's not a big deal.

01:52.360 --> 01:54.610
Now let's see that how this is going to work on.

01:55.120 --> 02:00.970
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

02:00.970 --> 02:02.770
into the array and that's pretty much it.

02:02.770 --> 02:08.560
And we're going to return the entirety of the message or we can just return it through that.

02:08.560 --> 02:10.210
Yeah, it was added something like that.

02:10.360 --> 02:16.240
So just like we have app dot get, we will have app post and yes similarly you can write, put and delete

02:16.240 --> 02:17.020
and all of that.

02:17.290 --> 02:19.510
So let's go ahead and work on with that.

02:19.510 --> 02:23.650
So how you're going to make sure that the data adds up, we first have to create a route for it.

02:23.650 --> 02:27.130
So this is going to be slash V one and slash.

02:27.130 --> 02:29.170
Let's call this one as ADD course.

02:29.180 --> 02:33.040
I feel free to call it as course if you want to follow those standards.

02:33.040 --> 02:34.930
But again, we'll talk about them later on.

02:35.410 --> 02:36.550
Request or response.

02:36.550 --> 02:37.840
We're going to go like this.

02:37.990 --> 02:41.830
Now, first thing is we need to access the things which are coming up.

02:41.830 --> 02:47.740
So in this case, a good idea would be to simply go ahead and log whatever is coming up as a request

02:47.740 --> 02:48.670
dot body.

02:48.670 --> 02:51.850
Now, this will be a great exercise for you to understand more stuff.

02:51.850 --> 02:54.400
I won't be doing it or let's just do it.

02:54.820 --> 02:58.000
And after that we're going to simply say courses.

02:58.570 --> 02:59.950
So this is my array.

03:00.040 --> 03:04.390
I'll use the property of dot push and I'll push it as request, dot body, whatever it is coming up

03:04.390 --> 03:04.870
in the body.

03:04.870 --> 03:06.250
I'm just pushing it up here.

03:06.490 --> 03:08.050
Now one more interesting thing.

03:08.080 --> 03:10.390
Usually in the cases, we don't push it like this.

03:10.390 --> 03:15.610
We do more refinements and more validation checks so that we know that what exactly is coming up and

03:15.610 --> 03:17.080
it is properly validated.

03:17.200 --> 03:21.370
Once this is done, we are going to say restart sin and we are going to just pass on a boolean, which

03:21.370 --> 03:23.500
is going to be true that the data was added.

03:23.500 --> 03:25.630
You can return the exact same body.

03:25.630 --> 03:28.120
You can also return the entirety of the course object.

03:28.120 --> 03:30.700
It's up to you how your architecture in your application.

03:31.060 --> 03:36.460
Okay, now let's move on to the swagger and let's see that how we can add a new course to the swagger.

03:36.460 --> 03:39.160
And we did called it up as ADD goes.

03:39.370 --> 03:39.810
Okay.

03:40.930 --> 03:46.750
So we're going to go ahead and copy this entirety of the stuff and let's go ahead and duplicate this.

03:47.860 --> 03:48.550
There we go.

03:48.550 --> 03:49.720
Nice and easy.

03:49.720 --> 03:50.380
There we go.

03:50.590 --> 03:52.960
Now, this one is not all of this.

03:52.960 --> 03:55.780
This is just add codes.

03:56.170 --> 03:58.090
Now, this is obviously not a get request.

03:58.090 --> 03:59.170
It is a post request.

03:59.170 --> 04:05.320
As soon as you do this as a post, you can actually go back into the browser and where is my browser?

04:05.320 --> 04:06.130
There we go.

04:06.160 --> 04:10.510
I can hit a reload and I can see that there should be a post request.

04:10.510 --> 04:11.080
So there we go.

04:11.080 --> 04:12.640
Add course is a post request.

04:12.640 --> 04:14.260
Yes, they are color coded nicely.

04:14.260 --> 04:15.580
You don't have to do any of that.

04:15.580 --> 04:16.540
So this is nice.

04:16.540 --> 04:20.230
Since we have made haven't made any other changes, this looks exactly the same.

04:20.230 --> 04:21.700
So let's go ahead and change that.

04:22.060 --> 04:25.150
So summary, let's go ahead and change this a little bit.

04:26.350 --> 04:34.570
Adds a new course to existing courses that makes sense.

04:34.690 --> 04:39.370
Now what kind of parameter you will be passing on now in this case, if the parameter is just request

04:39.370 --> 04:41.200
or query, you can go ahead and use that.

04:41.200 --> 04:44.590
But there are some additional additional lines that you can put up.

04:44.830 --> 04:49.540
These are two ones, what it's going to be consumes.

04:49.540 --> 04:50.650
So there we go.

04:51.250 --> 04:54.940
So what we are consuming is application JSON.

04:55.210 --> 05:00.100
And you can also optionally mention that what kind of things we produce produces.

05:01.170 --> 05:02.460
Produces.

05:02.550 --> 05:03.270
There we go.

05:03.300 --> 05:05.400
Hopefully I have not done any kind of typo.

05:05.490 --> 05:10.590
Now, usually what the output that comes up is usually in the recommended format is application JSON.

05:10.590 --> 05:15.600
In this case we are passing on a boolean, but just showing you that, yeah, this is the most ideal

05:15.600 --> 05:17.280
case scenario that you'll be going on.

05:17.520 --> 05:23.550
Now notice here we have the parameters, but the issue is that in the recent format, previously it

05:23.550 --> 05:28.850
was in the parameters if you are following the version two of it, but now it has been changed.

05:28.860 --> 05:32.820
I want to show you directly in the documentation so it removes all the confusion.

05:33.000 --> 05:37.080
So in here you can say that we have the request describing the request body.

05:37.320 --> 05:40.440
Let's go ahead and reload that notice here.

05:40.710 --> 05:43.950
Now it says all going up here.

05:44.310 --> 05:45.320
Here we go.

05:45.330 --> 05:46.920
It says request body.

05:46.920 --> 05:48.860
So now, previously it was all in.

05:48.870 --> 05:53.120
So if something is going in, in, we just say in params and in body and all of that.

05:53.130 --> 05:56.850
Now if you're sending something in the body, you have to define it as a request body.

05:56.850 --> 06:00.270
So make sure you confirm with the documentation one more time.

06:00.270 --> 06:01.890
So let's go ahead and change this one.

06:01.890 --> 06:04.620
In fact, we can change all of this.

06:04.620 --> 06:05.940
Yeah, we don't need all of this.

06:05.940 --> 06:07.740
So let's go ahead and write this one here.

06:07.890 --> 06:11.310
We're going to say we are passing on some data in request body.

06:11.310 --> 06:13.500
Make sure the B is capital in this case.

06:13.500 --> 06:14.220
There we go.

06:14.400 --> 06:16.800
Now what you're going to pass on the request body first.

06:16.800 --> 06:17.850
Is it required?

06:17.850 --> 06:19.890
Yes, this is very much required.

06:19.890 --> 06:24.210
So let's go ahead and mark this flag as true and then is content.

06:24.210 --> 06:27.150
So what kind of content you are passing on?

06:27.390 --> 06:31.050
The content that we are passing on is application JSON.

06:31.050 --> 06:36.450
You have to explicitly mention that and once this is done, all you got to do is define your schema.

06:36.450 --> 06:38.400
And this is we have done it many times.

06:38.400 --> 06:44.640
So schema actually we can copy paste this one because this is exactly the schema that's coming in and

06:44.640 --> 06:47.820
there are shortcut ways of defining these schemas like this.

06:48.000 --> 06:51.720
I'll mention that later on when we'll study the documentation a bit more.

06:51.930 --> 06:54.630
So let's go ahead and indent this properly.

06:54.630 --> 06:55.440
There we go.

06:56.600 --> 06:57.660
Okay, so that's it.

06:57.660 --> 07:00.900
The new thing that we have learned here is the request body that comes in.

07:00.900 --> 07:02.040
Let's go ahead and save this.

07:02.070 --> 07:05.190
Go back onto the browser here to reload on the swagger.

07:05.280 --> 07:06.420
And there we go.

07:07.140 --> 07:09.540
So it says could not render see the console.

07:09.540 --> 07:11.910
So definitely there is something wrong in the request body.

07:11.910 --> 07:13.500
Let me go ahead and quickly see that.

07:13.510 --> 07:14.100
Got it.

07:14.100 --> 07:15.390
It was really simple.

07:15.510 --> 07:17.250
We can see that here in the schema.

07:17.250 --> 07:18.660
We didn't guard the indentation.

07:18.660 --> 07:19.170
Right.

07:19.170 --> 07:21.630
So yes, it's a little bit of painful stuff.

07:21.630 --> 07:24.600
But after stating a little bit, you'll find it definitely.

07:24.600 --> 07:26.880
Let's go ahead and hit a reload and then we go.

07:26.880 --> 07:28.290
Now it's all happy.

07:28.290 --> 07:29.850
So we have a post request.

07:29.850 --> 07:32.760
It says Add course's description is nice.

07:32.760 --> 07:34.110
We have some parameters.

07:34.110 --> 07:35.610
We have no parameters in this one.

07:35.610 --> 07:39.060
You can additionally add your parameters to along with the request body.

07:39.060 --> 07:41.820
No problem in that we don't have a requirement of that.

07:41.820 --> 07:42.570
As of now.

07:42.600 --> 07:47.280
The schema you can find that we have an example nice up here that this is kind of a schema that you

07:47.280 --> 07:50.610
have to pass on and this is of application JSON type.

07:50.610 --> 07:55.110
The response is also going to be JSON type and this is how you're going to get a response.

07:55.110 --> 07:57.840
Now in this case, we are not getting a response like this.

07:57.840 --> 08:01.890
So what we can say that this is not coming in.

08:02.730 --> 08:09.420
Rather the type that is coming up is Boolean, save that, go back and hit reload again.

08:09.570 --> 08:13.110
And we are getting an example, true or false value that is coming in.

08:13.110 --> 08:15.360
So it's not really that tough told you.

08:15.750 --> 08:17.400
Let's go ahead and try it out now.

08:17.400 --> 08:19.440
It gives you the request body that you can pass on.

08:19.440 --> 08:21.660
So let's go ahead and add a55.

08:21.960 --> 08:27.570
The string is going to be a test string now since obviously it is not a persistent data, we are just

08:27.570 --> 08:30.150
throwing it up into the array.

08:30.150 --> 08:35.100
As soon as it hit reload or my back end reloads, it's going to just refresh and the pricing is going

08:35.100 --> 08:38.310
to be, let's just say just nine and let's execute that.

08:38.730 --> 08:42.600
And here we see that this is all going nicely into this URL.

08:42.720 --> 08:48.840
The response that we got is true and we can again verify that by requesting all the arrays.

08:48.840 --> 08:50.400
So let's go ahead and scroll back.

08:50.400 --> 08:53.490
And that is what I love absolute about these documentation.

08:53.490 --> 08:57.030
I can try out hit, execute and I can grab all the values.

08:57.030 --> 08:59.430
So notice this is the latest one that we have added.

08:59.430 --> 09:01.320
So cool, really awesome.

09:02.250 --> 09:07.260
So now that you know how to make a post request, also how you can request some data in the request

09:07.260 --> 09:10.770
body again reminding you it's not like you can only send request body.

09:10.770 --> 09:13.920
Your parameters can also be a part of this entire request.

09:13.920 --> 09:15.450
You can go ahead and add that one.

09:15.450 --> 09:17.490
Let's go ahead and catch up in the next video.
