WEBVTT

00:01.040 --> 00:05.870
Okay, Now let's create our famous Index.js file.

00:10.970 --> 00:12.740
Index.js.

00:12.860 --> 00:17.690
And then we are going to import request.

00:19.900 --> 00:23.440
So a quiet quest promise.

00:25.000 --> 00:33.280
And we are also going to create a method, just call it async function main.

00:38.790 --> 00:39.960
And.

00:42.190 --> 00:49.390
Then we are going to use a function or I'm sorry, options object.

00:49.810 --> 00:56.180
So we define an object here called options and we say the method is post.

00:56.200 --> 00:58.330
That's what we are using to log in.

00:58.570 --> 01:07.600
And then we put in a Uri and the Uri is the URL we got from when we are looking inside of this Firefox

01:07.600 --> 01:13.390
Facebook page where we looked at the action attribute inside of the form.

01:13.810 --> 01:16.210
And that's the URL we have here.

01:16.510 --> 01:20.680
Remember, it's Uri, they call it here, not URL.

01:22.270 --> 01:30.700
And then we have the form data, we call it form, and we use a pass in the email.

01:30.700 --> 01:38.200
And I'm going to make another file here called Credentials Dot, Email and Pass.

01:38.200 --> 01:40.800
I also say credentials dot pass.

01:40.810 --> 01:47.410
However, if you want to do this, you can just write your email out in a string like my email.

01:48.400 --> 01:51.160
Um, email.com.

01:51.160 --> 01:58.150
And I'm just doing this because I don't want to sit and edit my email and pass out in the video.

01:59.880 --> 02:04.080
So I'm going to make another file called Credentials and import it.

02:11.680 --> 02:12.760
And let's see.

02:12.760 --> 02:13.390
So

02:16.660 --> 02:19.720
here I'm going to type in my email.

02:26.060 --> 02:27.470
And my pass.

02:32.210 --> 02:35.960
And of course, you don't have to do this section.

02:35.960 --> 02:37.790
I'm just showing you what I'm doing.

02:37.790 --> 02:38.750
Exactly.

02:38.750 --> 02:42.170
So you understand what is going on here.

02:42.170 --> 02:42.890
So.

02:47.500 --> 02:49.240
The name Port The file.

02:51.680 --> 02:56.360
And then I can use it down here so I don't have to edit out my email all the time.

02:56.930 --> 03:01.010
And then we need to set that.

03:01.580 --> 03:03.260
Actually, I will not get to that yet.

03:03.290 --> 03:09.170
We will just try and complete the, um, the request now.

03:12.890 --> 03:20.810
So hold on a moment while I put in my actual credentials in the the the file here.

03:25.960 --> 03:32.290
Okay, so now I put in my actual Facebook email and password in the other file we defined here.

03:32.290 --> 03:37.810
And now we're going to try and actually complete this request we got here.

03:38.610 --> 03:47.580
So we say const result await request and then we pass in the options object that we defined here, which

03:47.580 --> 03:49.740
says it's a post and so on.

03:50.190 --> 03:53.280
And then we are going to.

03:54.430 --> 03:55.660
See what we get.

03:58.200 --> 04:01.170
Actually, I'm also going to write a catch clause here.

04:02.970 --> 04:04.180
Console error.

04:05.040 --> 04:05.580
Error.

04:12.610 --> 04:14.650
And I will write out error here.

04:14.650 --> 04:19.960
So I know it's an error, not just a regular result and.

04:21.360 --> 04:25.170
Okay, so let's try and see what happens now.

04:26.750 --> 04:28.980
We're making a post request.

04:29.000 --> 04:36.980
We're doing it in the same way with the form URL encoded as we did inside of Postman, and we're using

04:36.980 --> 04:41.990
the post method to this URL we got from the Firefox page.

04:42.950 --> 04:46.730
Now let's try and see what happens when we try and run it.

04:50.060 --> 04:54.170
And nothing happens because I'm not running the method.

04:55.280 --> 04:59.540
So let's type main down here in the global scope and run it again.

05:03.010 --> 05:07.360
So I get this huge object here, which is the response.

05:08.890 --> 05:11.050
And I get an error.

05:11.710 --> 05:18.730
And the reason we are getting an error is because it's returning a status code.

05:18.730 --> 05:21.130
302.

05:21.340 --> 05:24.280
And that's basically a redirect.

05:24.910 --> 05:35.420
Now inside of Postman, Postman is automatically redirecting the request so it gets to the right place.

05:35.440 --> 05:38.650
However, request doesn't do that automatically.

05:38.680 --> 05:41.380
It doesn't redirect the request automatically.

05:42.610 --> 05:50.200
Let me try and set a breakpoint here so you can see the how the response looks like.

05:50.200 --> 05:58.420
So I'm going into debugging in Visual Studio code and we will try and see how it looks like.

06:05.970 --> 06:06.840
So here we go.

06:06.840 --> 06:10.170
We are going now inside of the catch clause here.

06:10.170 --> 06:19.920
And if we go inside of error, we can see there's a status code 302, which is not really an error.

06:19.920 --> 06:27.930
It's more of a redirect, but request by default treats this as an error.

06:28.200 --> 06:36.780
But we can do a little option inside of it to make sure that we handle it the right way, which I will

06:36.780 --> 06:38.130
do in the next lecture.
