1 00:00:00,240 --> 00:00:04,100 In this video we're going to cover some more advanced postman features. 2 00:00:04,110 --> 00:00:09,930 That's going to make it a bit easier to use this tool especially as the number of requests we're working 3 00:00:09,930 --> 00:00:11,860 with continues to grow. 4 00:00:12,000 --> 00:00:17,730 So we're going to take a quick break from writing code and Visual Studio code and we'll focus on improving 5 00:00:17,730 --> 00:00:25,440 our postman workflow making it a bit easier to test out the requests that our API supports and to get 6 00:00:25,440 --> 00:00:25,960 started. 7 00:00:25,980 --> 00:00:32,310 When I want to talk about our postman environments and postman environment variables. 8 00:00:32,310 --> 00:00:37,610 To illustrate how environments are going to play a role in postman let's start off with an example. 9 00:00:37,620 --> 00:00:43,050 So right now all of our requests are being made to local host three thousand and that's good because 10 00:00:43,050 --> 00:00:45,210 that's where our server is running. 11 00:00:45,210 --> 00:00:52,500 Later on in the class though we're also going to deploy the task manager API to Heroku and in that case 12 00:00:52,500 --> 00:00:54,350 we'll have a different U.R.L.. 13 00:00:54,390 --> 00:01:00,690 We're also going to want to test those production and points from postman and that would mean US manually 14 00:01:00,900 --> 00:01:05,060 swapping out you are ELLs for a dozen or so different requests. 15 00:01:05,160 --> 00:01:10,830 I would have to go into each and change that you are all back and forth as I wanted to change which 16 00:01:10,830 --> 00:01:15,600 server I was making the request to with postman environments. 17 00:01:15,690 --> 00:01:19,290 We can set up an environment with various variables. 18 00:01:19,290 --> 00:01:22,890 These can be used to configure how postman runs. 19 00:01:22,890 --> 00:01:24,850 And that's exactly what we're gonna do. 20 00:01:24,900 --> 00:01:31,100 We're going to do it for you are elves and we're also going to do it for our authentication tokens. 21 00:01:31,170 --> 00:01:37,380 Let's get things started with a very basic example though we're going to focus on that create user end 22 00:01:37,380 --> 00:01:43,450 point we'll end up working down this list and they create user endpoint like all of them has the you 23 00:01:43,450 --> 00:01:50,120 are L hardcoded we're gonna address that now up above and the top right hand side we can see this know 24 00:01:50,130 --> 00:01:56,580 environment dropdown if I expand it we don't have any environments to pick from but we can go ahead 25 00:01:56,580 --> 00:02:03,450 and create our very first one right here I'll go ahead and set up an environment by clicking that gear 26 00:02:03,450 --> 00:02:07,210 icon which opens the manage environments panel. 27 00:02:07,290 --> 00:02:08,850 Now once again we don't have any. 28 00:02:08,880 --> 00:02:13,540 I can click Add to create the first one and right here we'll pick a name. 29 00:02:13,740 --> 00:02:19,960 I'll go ahead and use Task Manager API followed by death. 30 00:02:20,070 --> 00:02:24,600 So this will be the task manager API running in development mode. 31 00:02:24,720 --> 00:02:30,670 So all of the environment variables will be for that where the U.R.L. would be local host three thousand 32 00:02:31,170 --> 00:02:37,700 down below we can set up key value pairs for the various environment variables we want to create. 33 00:02:37,710 --> 00:02:45,880 For example I could create one called U.R.L. and I could set the value equal to local host three thousand. 34 00:02:45,900 --> 00:02:48,840 Let's take a quick moment to knock that out. 35 00:02:48,840 --> 00:02:51,610 Now we have initial value which is what we set. 36 00:02:51,720 --> 00:02:53,360 And then we have the current value. 37 00:02:53,370 --> 00:02:59,190 So it could get changed later on although in the case of U.R.L. it'll never get changed. 38 00:02:59,190 --> 00:03:05,760 So right here we can now click Add to create our brand new environment and we could always go ahead 39 00:03:05,760 --> 00:03:07,040 and create another. 40 00:03:07,080 --> 00:03:09,690 Let's take a quick moment to do that as well. 41 00:03:09,810 --> 00:03:15,980 Right here I can call this one task manager API. 42 00:03:16,000 --> 00:03:19,600 Then in parentheses abroad which is short for production. 43 00:03:19,810 --> 00:03:22,250 Now I don't know what the production you are ELAs. 44 00:03:22,270 --> 00:03:28,930 So I'll leave that blank for now though I will create the environment as a place holder down below I'll 45 00:03:28,930 --> 00:03:29,960 click add. 46 00:03:29,980 --> 00:03:32,500 And now we have two environments to pick from. 47 00:03:33,190 --> 00:03:40,120 If I close the environments modal pop up I can go back to that no environment dropdown and I can see 48 00:03:40,120 --> 00:03:41,590 my two options. 49 00:03:41,710 --> 00:03:47,890 So I can click over to this one to start running requests on the dev server and I can click over to 50 00:03:47,920 --> 00:03:52,200 this one to start running requests on the production server. 51 00:03:52,210 --> 00:03:58,120 Now let's go ahead and actually figure out how to use that you are well variable we set up so I'm going 52 00:03:58,120 --> 00:04:04,420 to switch back over to the dev environment and we're going to adjust the U.R.L. we have right here. 53 00:04:04,460 --> 00:04:08,410 Oh we're gonna do is take local host three thousand and remove it. 54 00:04:08,590 --> 00:04:14,860 We're still going to leave the forward slash users as that part the path will change for every request 55 00:04:15,250 --> 00:04:19,220 right here to use and environment variables value. 56 00:04:19,300 --> 00:04:23,680 We start by opening and closing two sets of curly braces. 57 00:04:23,830 --> 00:04:26,230 Then we name the environment variable. 58 00:04:26,260 --> 00:04:33,330 We're trying to use in this case it's you are El now with this in place when we make the request that 59 00:04:33,480 --> 00:04:38,430 you are El value will be used and the dev environment has that defined. 60 00:04:38,430 --> 00:04:44,680 If I actually hover over that little part I can see the value that's going to be used which is fantastic. 61 00:04:44,680 --> 00:04:50,850 And if I go ahead and send this off it's actually going to work as expected I'll click save first then 62 00:04:50,850 --> 00:04:56,880 send and what do I get down below it's actually able to communicate with the server correctly even though 63 00:04:56,880 --> 00:04:58,830 that email is already take. 64 00:04:58,890 --> 00:05:04,000 Now if we were to switch over to the production environment all our requests would fail because there's 65 00:05:04,020 --> 00:05:07,710 no you are well defined and that's OK for now from here. 66 00:05:07,740 --> 00:05:13,110 Let's go ahead and work down the list making sure we use the you are El environment variable. 67 00:05:13,110 --> 00:05:14,240 So I have a log in. 68 00:05:14,280 --> 00:05:18,860 I'll go ahead and update that and then I'll go ahead and save things. 69 00:05:18,990 --> 00:05:21,320 Next up in the list we have create task. 70 00:05:21,330 --> 00:05:25,470 I'll make sure to use the environment variable there and save it. 71 00:05:25,590 --> 00:05:35,530 We have read profile after that we have a read user from there we have read tasks. 72 00:05:35,630 --> 00:05:42,790 Next up we have read at task individual then we have the ability to update a user. 73 00:05:42,950 --> 00:05:48,890 We're going to swap out the value there and just three more left update task and then we move on to 74 00:05:48,890 --> 00:05:50,650 our delete end points. 75 00:05:50,810 --> 00:05:55,200 I have delete user and I have delete task. 76 00:05:55,390 --> 00:05:58,210 Now if you thought that was annoying you're right. 77 00:05:58,210 --> 00:06:01,740 And that's the whole reason we're setting up these environment variables. 78 00:06:01,750 --> 00:06:04,250 Yes we had to do it once but it's done. 79 00:06:04,390 --> 00:06:10,330 And now when we want to swap out the U.R.L. in the future we'll never need to manually do that again. 80 00:06:10,330 --> 00:06:16,810 We can change the environment variable a single time or switch between environments to have all of our 81 00:06:16,810 --> 00:06:19,300 requests automatically update. 82 00:06:19,300 --> 00:06:22,960 So that's the first thing I wanted to accomplish in this video. 83 00:06:22,960 --> 00:06:27,050 The next one is a little more complex and also a little more useful. 84 00:06:27,160 --> 00:06:30,380 And it has to do with how we're setting up authentication. 85 00:06:30,500 --> 00:06:36,760 So over here read profile is currently the only request that actually has authentication set up and 86 00:06:36,790 --> 00:06:41,320 to authenticate it's providing the token as part of a header. 87 00:06:41,320 --> 00:06:45,460 Now we can continue to do that but there are better ways to get the job done. 88 00:06:45,550 --> 00:06:49,060 If I fire this off yes it works. 89 00:06:49,060 --> 00:06:53,680 But now if I wanted to add authentication to my other requests it would fail. 90 00:06:53,680 --> 00:06:59,350 Let's go ahead and talk about how we can update multiple requests to all use the same authentication 91 00:06:59,350 --> 00:07:00,260 scheme. 92 00:07:00,310 --> 00:07:03,860 So right here we're going to grab that token value. 93 00:07:03,970 --> 00:07:07,390 We're not going to grab the space or the bearer keyword. 94 00:07:07,390 --> 00:07:13,230 We just want the JWT and we're gonna go ahead and delete this header completely. 95 00:07:13,240 --> 00:07:19,370 Now this is going to cause the request to fail which is OK we're not authenticated and that's expected. 96 00:07:19,540 --> 00:07:24,400 What we're going to do from here is head over to the authorization tab. 97 00:07:24,490 --> 00:07:29,680 This provides us with a different way to set up authorization and it's only different in terms of the 98 00:07:29,680 --> 00:07:36,850 user interface in the end of the day the exact same request is getting sent from postmen to the Express 99 00:07:36,910 --> 00:07:40,060 server from this type dropdown. 100 00:07:40,150 --> 00:07:44,380 We're going to switch it up to the following which is bearer token. 101 00:07:44,380 --> 00:07:50,200 That's the strategy we're using where we provide the authentication token and then we can go ahead and 102 00:07:50,200 --> 00:07:52,080 paste that right in here. 103 00:07:52,120 --> 00:07:58,060 This is just a different way to get done exactly what we were getting done before I go ahead and save 104 00:07:58,060 --> 00:07:59,110 that request. 105 00:07:59,110 --> 00:08:02,630 I fire it off and now we're back to a working app. 106 00:08:02,650 --> 00:08:03,950 Now this does indeed work. 107 00:08:03,970 --> 00:08:08,140 But once again it's only working for this specific request. 108 00:08:08,140 --> 00:08:10,650 So this is not the approach we're going to take. 109 00:08:10,840 --> 00:08:18,660 What we're gonna do is switch the type back to the type we had before which is inherit off from parent. 110 00:08:18,670 --> 00:08:25,330 This allows us to define our authentication scheme once and use it in every single request that has 111 00:08:25,330 --> 00:08:27,690 this type under authorization. 112 00:08:27,760 --> 00:08:32,410 And it's the default type which means all of them to actually get this done. 113 00:08:32,440 --> 00:08:39,010 All we have to do is take our authentication token over to the following menu for the task collection 114 00:08:39,040 --> 00:08:42,160 will click that drop down menu from here. 115 00:08:42,160 --> 00:08:47,700 We're gonna go to edit and from edit we're going over to the authorization tab. 116 00:08:47,710 --> 00:08:50,750 Now this is exactly what we just saw before. 117 00:08:50,860 --> 00:08:57,370 But instead of setting up authorization for a single request we're setting it up for every single request 118 00:08:57,430 --> 00:08:58,600 in this collection. 119 00:08:58,720 --> 00:09:00,960 Right here we're going to switch from no. 120 00:09:00,960 --> 00:09:06,190 Off to bearer token and we're going to provide that token value. 121 00:09:06,280 --> 00:09:08,530 I'm going to update the collection. 122 00:09:08,530 --> 00:09:16,270 And with that in place any request that has authorization set to inherit is going to be able to authenticate 123 00:09:16,570 --> 00:09:19,180 without needing to do anything manually. 124 00:09:19,180 --> 00:09:22,710 So right here I can send this off and what do I get. 125 00:09:22,720 --> 00:09:28,240 I'm still authenticated even though it's not set up anywhere in the individual request. 126 00:09:28,240 --> 00:09:32,980 I can see under header it's showing up a little bit but this is just showing up because it's giving 127 00:09:32,980 --> 00:09:37,130 us a preview of that collection header we had set before. 128 00:09:37,150 --> 00:09:42,580 So now that we have this in place all of our requests are going to be authenticated but we can still 129 00:09:42,580 --> 00:09:44,560 customize things further. 130 00:09:44,560 --> 00:09:50,830 We know that create user and log in user don't require authentication even though their authorization 131 00:09:50,830 --> 00:09:53,680 value is inherit by default. 132 00:09:53,680 --> 00:09:55,480 We can always switch that up to no. 133 00:09:55,480 --> 00:09:58,790 Off to disable authentication for both of those. 134 00:09:58,840 --> 00:10:04,570 I'll go ahead and disable it for create user and say that and I'll do the exact same thing for log in 135 00:10:04,840 --> 00:10:07,850 updating authorization over to No. 136 00:10:07,850 --> 00:10:15,370 OK now for all of the other requests as an example update user that's already the authorization value 137 00:10:15,580 --> 00:10:19,600 so all of our requests are inheriting from the off parent. 138 00:10:19,600 --> 00:10:26,080 So they're getting that token set correctly except for sign up and log in and that is exactly what we 139 00:10:26,080 --> 00:10:27,110 want. 140 00:10:27,130 --> 00:10:31,620 Now there's just one more thing we're going to do to take this to the next level. 141 00:10:31,720 --> 00:10:37,630 And this is going to require us to set up a little bit of JavaScript code to perform some automation. 142 00:10:37,630 --> 00:10:40,450 So what's the one manual part of this workflow. 143 00:10:40,450 --> 00:10:47,350 The manual part is either creating a user and logging in then getting that off token going back over 144 00:10:47,350 --> 00:10:52,340 to that menu to update the value and then being able to make their requests. 145 00:10:52,480 --> 00:10:58,270 We can have all of that done automatically for us by writing just three or four lines of javascript 146 00:10:58,270 --> 00:10:59,130 code. 147 00:10:59,200 --> 00:11:04,810 Now all the code we're going to write is going to live in the following menu for both create user end 148 00:11:04,840 --> 00:11:08,110 log in user it is the tests menu. 149 00:11:08,110 --> 00:11:14,680 So we have pre request script where we can add some javascript code to run before the request is sent 150 00:11:14,680 --> 00:11:15,310 off. 151 00:11:15,310 --> 00:11:19,210 In this case before the log in at request is sent off. 152 00:11:19,210 --> 00:11:20,860 We also have tests. 153 00:11:20,980 --> 00:11:28,510 Now this runs after the response is received and in here we can write some javascript code to extract 154 00:11:28,540 --> 00:11:36,260 that token property on the body and set an environment variable whose value is equal to that token. 155 00:11:36,460 --> 00:11:41,920 From there we'll use the environment variable inside of the collection to get everything working as 156 00:11:41,920 --> 00:11:43,100 expected. 157 00:11:43,120 --> 00:11:45,240 So let's start by editing our collection. 158 00:11:45,240 --> 00:11:52,330 Just one more time right here under authorization no longer are we actually going to list out a Jason 159 00:11:52,330 --> 00:11:53,110 web token. 160 00:11:53,560 --> 00:11:59,270 Instead we're going to reference an environment variable we'll call this off token. 161 00:11:59,350 --> 00:12:05,530 Now it currently doesn't exist but we're about to write some code to set it so I will update the collection 162 00:12:06,400 --> 00:12:13,450 and we're going to start in the tests tab for the log in user request right here the first thing we 163 00:12:13,450 --> 00:12:16,720 want to do is make sure that the request succeeded. 164 00:12:16,720 --> 00:12:23,620 We want to make sure we got a 200 status code we can use and if condition to check that right here if 165 00:12:24,250 --> 00:12:31,030 and postman gives us access to an object P. M. We can use when we're writing our little pre request 166 00:12:31,030 --> 00:12:38,830 script or test code in this case we can use something on p.m. to get the status code it is p.m. which 167 00:12:38,830 --> 00:12:43,490 is short for a postman dot response dot code. 168 00:12:43,510 --> 00:12:49,030 This is a number and we're going to check and see if it's two hundred if it is two hundred will run 169 00:12:49,030 --> 00:12:56,940 the code that actually sets the environment variable now in this case PND contains another thing we 170 00:12:56,940 --> 00:13:06,460 can use to set the environment variable that is M. dot environment p.m. dot environment contains a method 171 00:13:06,550 --> 00:13:13,540 set and we can use this to set the value for an environment variable in our current environment. 172 00:13:13,750 --> 00:13:20,050 So we have a key value pair where both are strings we have the environment variable name we're trying 173 00:13:20,050 --> 00:13:20,950 to set. 174 00:13:20,950 --> 00:13:27,910 We just call that auth token before and then we have the value we're trying to use now in this case 175 00:13:27,950 --> 00:13:34,570 we know the value it lives on that response body so down below we have the response for one of our previous 176 00:13:34,570 --> 00:13:40,810 requests here is the body and at the very bottom we have that token property this is the value that 177 00:13:40,810 --> 00:13:44,950 we need to get but we need to get it from our test script. 178 00:13:44,950 --> 00:13:50,950 So right here instead of passing in a string like we were doing previously we're gonna go ahead and 179 00:13:50,950 --> 00:13:53,180 grab the code from somewhere else. 180 00:13:53,290 --> 00:14:00,770 We're going to access something else on p. m. It is P. M. dot response dot Jason. 181 00:14:01,180 --> 00:14:07,840 This is going to take the Jason response and convert it to an object and the object is the return value. 182 00:14:07,870 --> 00:14:11,620 So on this object the only thing we need access to is token. 183 00:14:11,980 --> 00:14:14,650 So dot token and there we go. 184 00:14:14,710 --> 00:14:19,240 Now we have a script that's going to set the authentication token automatically. 185 00:14:19,300 --> 00:14:20,980 Anytime we log in. 186 00:14:20,980 --> 00:14:27,880 So if our token expires I can run log in a single time then I can go to any of the other requests to 187 00:14:27,880 --> 00:14:30,920 run them and make sure things are working as expected. 188 00:14:30,940 --> 00:14:32,970 Let's go ahead and test this out. 189 00:14:33,010 --> 00:14:39,040 I'm going to move over to read profile and I'm going to run this request before I ever run log in when 190 00:14:39,040 --> 00:14:42,250 I do I'm not authorized which is expected. 191 00:14:42,340 --> 00:14:46,570 Now I run log in which should automatically set that token for me. 192 00:14:46,570 --> 00:14:50,510 The log in request finished and I'll head back over to read profile. 193 00:14:50,590 --> 00:14:52,780 This time it should work. 194 00:14:52,780 --> 00:14:57,870 I'll fire it off for a second time and it does indeed work which is fantastic. 195 00:14:57,880 --> 00:15:04,060 Now the last thing we're going to do is take our code in this test tab and we're going to bring it up 196 00:15:04,060 --> 00:15:11,800 to create user right here we'll go over to the tests tab again I'll paste the exact same code in and 197 00:15:11,800 --> 00:15:17,050 the only thing I'm going to do is change the status code we're looking for when we create a user we 198 00:15:17,050 --> 00:15:21,220 get a two a one back when things work well so we can save that. 199 00:15:21,250 --> 00:15:22,730 And now we're done. 200 00:15:22,750 --> 00:15:25,870 So now we have a completely automated system. 201 00:15:25,900 --> 00:15:32,740 Yes we have to log in once but everything else is handled behind the scenes for us no longer are we 202 00:15:32,740 --> 00:15:38,860 gonna have to copy and paste values around or manually change you or else this is going to make it a 203 00:15:38,860 --> 00:15:45,370 lot easier to continue to use postman especially as we set up authentication for the other end points 204 00:15:45,490 --> 00:15:47,200 that our API supports. 205 00:15:47,200 --> 00:15:53,110 Let's take a quick moment to head back over to that gear go back to the dev environment by clicking 206 00:15:53,110 --> 00:15:53,680 on it. 207 00:15:53,830 --> 00:15:59,770 And here we can see that you are well has the same value as before and off token has the current value 208 00:15:59,800 --> 00:16:06,520 equal to this what we're seeing over here the J WTI there's no initial value because we never manually 209 00:16:06,520 --> 00:16:08,980 typed anything in here and that's fine. 210 00:16:09,040 --> 00:16:14,800 There is no default auth token we would want it should get set as the response from either signing up 211 00:16:14,830 --> 00:16:16,330 or logging it. 212 00:16:16,330 --> 00:16:22,030 So let's take a quick moment to recap because we covered quite a bit in this video we started by creating 213 00:16:22,030 --> 00:16:28,720 postmen environments and environment is nothing more than a name for your environment and a key value 214 00:16:28,720 --> 00:16:32,950 pair of values you can access when working in that environment. 215 00:16:32,950 --> 00:16:38,350 So in this case under the dev environment and for the task manager API we have you are well. 216 00:16:38,380 --> 00:16:44,860 And we have token if I go back to the menu and go over to prod we have nothing and that's OK we'll focus 217 00:16:44,860 --> 00:16:46,880 on setting that up later. 218 00:16:46,900 --> 00:16:50,130 Now once we have our environments in place we can switch between them. 219 00:16:50,140 --> 00:16:56,060 The other menu up top and we can access those environment variables from anywhere such as that you are 220 00:16:56,060 --> 00:17:03,430 L right here or such as the collections edit page where we set up authentication for every single request 221 00:17:03,550 --> 00:17:07,660 in the collection except for signing up or logging in. 222 00:17:07,660 --> 00:17:12,490 Now if you have any questions about what was covered here or you're not clear on why we're doing something 223 00:17:12,730 --> 00:17:18,640 just crack open a new question in the Q and A otherwise I'll see you in the next lesson where we get 224 00:17:18,640 --> 00:17:23,130 back to visual studio code and we continue to build out the API. 225 00:17:23,410 --> 00:17:25,300 Let's go ahead and jump right in.