1 00:00:00,960 --> 00:00:09,500 Welcome back. Let's try and get this 'post' request to work, that we couldn't get in the previous video. 2 00:00:09,740 --> 00:00:16,100 Now in order for us to test something more than just GET, well we can use the Terminal but there's a 3 00:00:16,100 --> 00:00:21,570 really nice tool that allows us to make these requests to a server and see what we get back. 4 00:00:21,920 --> 00:00:25,690 And it's called Postman. 5 00:00:25,790 --> 00:00:33,620 If you go to www.getpostman.com it is one of the best tools if you're ever working 6 00:00:33,620 --> 00:00:34,310 with a server. 7 00:00:34,310 --> 00:00:38,910 It's free and you can download it for your operating system. 8 00:00:39,080 --> 00:00:42,320 Just simply click on whichever one you want. 9 00:00:42,320 --> 00:00:49,180 Sign up and once it's loaded then you can open it up and you'll have something like this. 10 00:00:50,330 --> 00:00:53,040 And don't worry it's very very simple to use. 11 00:00:53,120 --> 00:00:58,150 We simply have here the requests that we want to get. 12 00:00:58,190 --> 00:01:02,030 And you'll see a few that you've never seen before and don't worry about them. 13 00:01:02,030 --> 00:01:04,970 These are very very rarely used. 14 00:01:04,970 --> 00:01:08,810 We see the ones that we care about DELETE, PUT, POST, GET right there 15 00:01:11,640 --> 00:01:15,630 and you simply enter the URL that you want to have this running on. 16 00:01:16,110 --> 00:01:19,130 So in our example let's do 'npm start' again. 17 00:01:20,850 --> 00:01:25,370 And while the server is running let's just go to '/profile'. 18 00:01:25,620 --> 00:01:34,720 And then in here we want to say 'localhost:3000' – that's the port that we gave it – and we'll say 19 00:01:34,760 --> 00:01:36,320 '/profile' 20 00:01:36,420 --> 00:01:38,460 So we want to do a GET request over here. 21 00:01:38,520 --> 00:01:50,500 If I click send I get 'getting profile'. It gives me the Headers and the Body, which is great, but now because 22 00:01:50,500 --> 00:01:54,110 we have this tool we can also do a POST request. 23 00:01:54,190 --> 00:01:59,250 So let's say that we are no longer sending the 'user'. 24 00:01:59,740 --> 00:02:02,820 We want to send a response [res]. 25 00:02:05,590 --> 00:02:10,789 And from this response we want to return whatever user that we submit to '/profile'. 26 00:02:14,060 --> 00:02:14,960 So let's start off here, first. 27 00:02:14,960 --> 00:02:19,960 We're going to change this to a POST and with a POST 28 00:02:20,090 --> 00:02:26,780 remember that we can actually add things to the body and you see over here that there's a couple of 29 00:02:26,900 --> 00:02:37,800 options: 'form-data' the 'x-www-form-urlencoded' we have 'raw' and then 'binary'. 30 00:02:37,850 --> 00:02:39,160 Now don't let this scare you. 31 00:02:39,170 --> 00:02:43,390 These two are forms through HTML. 32 00:02:43,460 --> 00:02:51,230 If you usually do an HTML form tag and do a GET or a POST request you can just click on this and you 33 00:02:51,230 --> 00:02:58,620 can see here 'Key Value' so I can say name equals 'andrei'. 34 00:02:58,710 --> 00:03:09,330 And now when I send this I get 'Sally' and 'soccer' well because well I haven't saved this yet let's 35 00:03:09,330 --> 00:03:18,050 go back to what we had before and send that again we see that the POST request is working. 36 00:03:18,150 --> 00:03:22,650 And if I want to access this information I simply do 37 00:03:27,150 --> 00:03:29,520 '(req.body)' 38 00:03:29,860 --> 00:03:31,960 But let's see if this works. 39 00:03:32,260 --> 00:03:33,190 If I click send 40 00:03:37,590 --> 00:03:45,230 Hmm I get 'undefined' and that is because Express only gives you a little bit. 41 00:03:45,270 --> 00:03:48,470 You still need a few packages if you want this to work. 42 00:03:48,630 --> 00:03:58,200 If you ever want to access '(req.body)' well you need to use a middleware and the middleware that 43 00:03:58,200 --> 00:04:03,900 we want is called body-parser and you'll use it and most Express servers, so you'll just get used 44 00:04:03,900 --> 00:04:04,470 to it. 45 00:04:04,470 --> 00:04:09,110 It's 'npm install body-parser'. 46 00:04:09,360 --> 00:04:14,970 And now this body-parser what it's going to do is it's going to grab whatever information we receive 47 00:04:15,510 --> 00:04:26,440 and parse it just like JSON or forms into something that we can use. In order to actually use it 48 00:04:27,400 --> 00:04:32,080 well, we know how middleware works, we simply at the top say 'app.use()' 49 00:04:35,320 --> 00:04:48,540 and body-parser it will come with a 'bodyParser' that we'll just require with the package that we just 50 00:04:48,540 --> 00:04:48,960 installed. 51 00:04:53,740 --> 00:04:59,010 In 'app.use()' we simply do 52 00:04:59,040 --> 00:05:01,040 'bodyParser.urlencoded' 53 00:05:02,450 --> 00:05:06,880 Which is how we're able to get this ['x-www-form-urlencoded']. 54 00:05:07,040 --> 00:05:12,290 And we just have to pass it a parameter: {extended: false}. 55 00:05:12,360 --> 00:05:13,150 Let's try that again. 56 00:05:13,200 --> 00:05:13,970 I'm going to save 57 00:05:17,200 --> 00:05:24,470 we'll do 'npm start' if we go back and send again. 58 00:05:24,640 --> 00:05:25,440 We see that. 59 00:05:25,480 --> 00:05:26,940 There you go it's working. 60 00:05:26,940 --> 00:05:33,940 {name: 'andrei'} because we've sent our 'Key Value' with form data. 61 00:05:34,010 --> 00:05:40,620 So if you're building a server for a form that gets submitted you'll use this. 62 00:05:40,770 --> 00:05:47,970 But as you know there's also JSON that we can use and the way we use JSON is we click on 'raw' and 63 00:05:47,970 --> 00:05:57,000 in here we can send whichever things we want; in our case it's going to be JSON, and with JSON we can 64 00:05:57,000 --> 00:06:06,590 now say what we want to send, and if we ever do any wrong syntax, it'll tell us that this is wrong JSON. 65 00:06:06,780 --> 00:06:14,290 But let's just say for the '/profile' I want to add a new user and this user will be called – remember 66 00:06:14,290 --> 00:06:20,810 double quotes here – we'll have "user" and this user will be called "Jenny" 67 00:06:24,540 --> 00:06:28,860 and "Jenny" will have a hobby of "tennis" 68 00:06:36,550 --> 00:06:37,590 Everything looks fine. 69 00:06:37,600 --> 00:06:40,540 That's JSON. If I now send this 70 00:06:45,660 --> 00:06:54,600 I get an empty object. Again because we've only told it to parse 'urlencoded' body-parser needs 71 00:06:54,600 --> 00:07:06,020 to also know about JSON. Again in order to do that we say 'app.use(bodyParser.json())' 72 00:07:06,020 --> 00:07:06,740 If we save that 73 00:07:09,380 --> 00:07:18,510 and send again - look at that - we have { user: 'Jenny', hobby: 'tennis' }. 74 00:07:18,590 --> 00:07:28,590 So now we can add that user by maybe adding it to our database and then sending a response 'Success' 75 00:07:31,800 --> 00:07:33,500 Let's try that again. 76 00:07:33,690 --> 00:07:41,340 We get a response 'Success' that we've posted "Jenny" "tennis" in this way we can also do PUT, which is very 77 00:07:41,340 --> 00:07:48,180 similar we just send the data but PUT is used for updating if we want to update perhaps Jenny to have 78 00:07:48,180 --> 00:07:56,420 a different hobby and DELETE to delete maybe that user, something that we'll get into in the next video. 79 00:07:56,490 --> 00:07:58,730 But as you can see Postman is great. 80 00:07:58,860 --> 00:08:03,810 It's a great way to test out your server before you connect it to your front-end, something that we'll 81 00:08:03,810 --> 00:08:08,600 definitely do in this course in our final project. 82 00:08:08,600 --> 00:08:09,950 Very cool. 83 00:08:10,010 --> 00:08:11,510 I'll see you in the next one. 84 00:08:11,520 --> 00:08:12,300 Bye-bye.