WEBVTT

00:00.290 --> 00:06.230
Okay, so we can test our API endpoints in the browser, which has limited utility because we'll soon

00:06.230 --> 00:08.150
find it frustrating to work with.

00:08.150 --> 00:11.420
And there are better tools out there to test our API.

00:11.450 --> 00:17.000
By the way, I'll mention it now, because you may have seen a file in your project called API dot http,

00:17.300 --> 00:20.720
and this is a way of testing API endpoints.

00:20.750 --> 00:24.710
Now in order to use it in VS code, we would need to add an extension.

00:24.710 --> 00:30.020
I believe in Visual Studio you can just right click and run this file and it will test your endpoints.

00:30.020 --> 00:37.490
But you would need to continuously add new entries into this particular file to use this facility to

00:37.520 --> 00:38.960
test your API.

00:39.500 --> 00:44.360
It works and you might want to use it, but I'm not going to demonstrate it or use it in this course.

00:44.360 --> 00:50.390
So I'm just going to delete it because we're going to be using postman to make the tests and API requests

00:50.420 --> 00:52.610
to our API controller endpoints.

00:52.610 --> 00:56.120
So please do open up postman which you should have installed.

00:56.120 --> 01:03.320
I mentioned installing this in the setup lesson and once you open postman you should see something like

01:03.320 --> 01:03.560
this.

01:03.590 --> 01:09.160
It gets updated quite frequently so it may change slightly, but the principles of what we're looking

01:09.160 --> 01:11.680
at will always pretty much stay the same.

01:11.680 --> 01:14.200
So I'm going to create a new workspace.

01:14.200 --> 01:19.390
I have a number of workspaces from various other projects, and I'll create a clean workspace to work

01:19.390 --> 01:20.110
in.

01:20.530 --> 01:22.570
And I'm just going to use the Create workspace.

01:22.570 --> 01:30.730
Create a blank workspace, click next and give it a name of Restore Course and only me.

01:30.850 --> 01:32.380
I don't need to share this with anyone else.

01:32.380 --> 01:34.240
And I'm going to click create.

01:34.330 --> 01:37.300
And this gives us a workspace that we can use.

01:37.300 --> 01:40.750
So postman works with a concept of collection.

01:40.750 --> 01:45.370
So I'm going to say new and I'm going to say collection a collection of requests.

01:45.370 --> 01:48.160
And give it a name of just test for the time being.

01:48.160 --> 01:54.310
And then I can add an example request and we can give our request a name.

01:54.310 --> 01:56.200
I'm just going to give it a name of weather.

01:56.200 --> 01:57.580
We'll just use this one.

01:57.580 --> 02:00.790
Just an example to test the weather forecast controller.

02:00.790 --> 02:04.480
And we can choose what type of request I'm going to make a Get request.

02:04.480 --> 02:13.390
And then we can specify the URL which is going to be https colon forward slash forward Local host.

02:13.390 --> 02:15.100
Colon 5001.

02:15.130 --> 02:17.560
Forward slash API, not API.

02:17.590 --> 02:21.850
Forward slash weather forecast for list one.

02:22.270 --> 02:24.520
And then I'm going to click send.

02:24.520 --> 02:28.270
And this goes and gets the summary of weather forecasts.

02:28.570 --> 02:29.050
Great.

02:29.050 --> 02:35.050
But we also have in postman I'm just going to demonstrate this very briefly is we've got the options

02:35.050 --> 02:36.580
about how we send up our request.

02:36.610 --> 02:39.580
We can send a query string parameters inside here.

02:39.580 --> 02:44.170
If I had a query string I could start saying test and then value.

02:44.200 --> 02:50.770
And notice the URL gets updated with these and I can remove them just as easily.

02:51.040 --> 02:52.840
We also have authorization.

02:52.840 --> 02:54.910
How do we want to authorize our request.

02:54.940 --> 02:56.410
We'll talk about that later.

02:56.440 --> 02:59.050
Any headers that we wish to add with the request.

02:59.050 --> 03:01.420
Anything we want to send up in the body of the request.

03:01.450 --> 03:03.250
We can do so inside here.

03:03.610 --> 03:08.980
And we've got some scripts so we can do something before the request goes and comes after.

03:09.010 --> 03:11.170
We'll take a look at that if we need to.

03:11.410 --> 03:14.890
And we can also do tests inside here as well.

03:14.890 --> 03:21.480
And also, just one other thing I'll mention before I go any further is the console, so we can get

03:21.480 --> 03:23.850
details about the raw request itself.

03:23.850 --> 03:26.610
If we're troubleshooting, we might want to come inside here.

03:26.610 --> 03:31.530
And then we've got access to the request headers, the response headers, the response body, everything

03:31.560 --> 03:34.800
to do with the request in its raw format.

03:34.800 --> 03:37.230
And we might want to do that.

03:37.800 --> 03:42.690
We will see warnings inside here because dotnet uses a self-signed certificate.

03:42.690 --> 03:46.080
Postman is not able to verify it's self-signed certificates.

03:46.080 --> 03:49.200
So don't worry about seeing this yellow color.

03:49.200 --> 03:55.350
It's a warning and we cannot do anything about it because we cannot tell postman to trust our dotnet

03:55.350 --> 04:02.100
certificate, and it doesn't respect whatever we do with our computer in terms of self-signed certificates.

04:02.100 --> 04:04.230
So don't worry about this warning.

04:04.560 --> 04:11.160
Now I've gone ahead and pre-created a collection to make it faster for us to test our API endpoints

04:11.160 --> 04:12.660
as we create them.

04:12.660 --> 04:18.540
So if you go to the course assets folder, once again there's a postman folder, and there's a postman

04:18.540 --> 04:21.440
collection that we can use to test this.

04:21.440 --> 04:25.520
So if we go to postman and let's see.

04:25.550 --> 04:32.570
And we click on the import button, then we can drop in the postman collection that I have provided.

04:32.570 --> 04:36.680
And I'm just going to drag it in to there.

04:36.860 --> 04:42.770
And then we've got our restore collection with all of the different requests that I've pre-created inside

04:42.770 --> 04:42.980
here.

04:43.010 --> 04:46.550
Now there's a couple of things I'll point out as well, just to make our lives a bit easier.

04:46.580 --> 04:50.300
Inside the collection level called restore, we've got some variables.

04:50.330 --> 04:59.000
These are collection level variables, and it's already set to https localhost 5001 for the value URL.

04:59.000 --> 05:04.400
And if I go to one of these requests, not this one, but get the product list, then instead of localhost

05:04.400 --> 05:09.740
5001, I've just replaced it with this variable in a double curly brackets.

05:09.770 --> 05:16.700
And that means if I do change my URL, I only need to update it in one place here.

05:16.700 --> 05:19.430
And then I can test the API endpoints.

05:19.430 --> 05:24.470
So this first one is going to be a Get request to localhost 5001.

05:24.470 --> 05:25.760
So I'm going to click send.

05:25.790 --> 05:31.010
And what we should find is we get the 200, okay, along with our list of products.

05:31.040 --> 05:34.640
If I go to get a single product, I've got API products two.

05:34.670 --> 05:38.570
And if I click send, then I get product with the ID of two back.

05:38.570 --> 05:45.560
And if I say products with the ID of 22, then what we'd expect to see is a 404 not found.

05:45.560 --> 05:50.420
So I'll click send and we get the correct response back from our API server.

05:50.420 --> 05:53.090
So that's what we should be looking at right now.

05:53.090 --> 05:57.980
And we've got a bunch of requests created for other things that we're going to look at as we get to

05:57.980 --> 05:59.900
it in the training course.

05:59.900 --> 06:06.620
So this will just speed us up as we create the code and want to make sure something is working as expected,

06:06.980 --> 06:09.920
it will come back and use these as and when we need to.

06:09.950 --> 06:14.090
So what we're going to take a look at next though is we're going to take a look at our API controller

06:14.090 --> 06:14.900
again.

06:14.900 --> 06:19.700
And what we're doing here is synchronous code to go to our database.

06:19.700 --> 06:21.050
But there is a different option.

06:21.050 --> 06:28.880
And I want to discuss and change this to be asynchronous for when we're making queries to our database.

06:28.940 --> 06:31.730
And I'll explain why next.
