1 00:00:00,400 --> 00:00:03,936 We need more than just a status code to complete our goal of getting a student's 2 00:00:03,936 --> 00:00:05,430 profile information. 3 00:00:05,430 --> 00:00:09,600 So we can retrieve their badge count and total number of JavaScript points. 4 00:00:09,600 --> 00:00:13,153 We can get more info from the body of the response. 5 00:00:13,153 --> 00:00:14,510 Let's go back to the docs. 6 00:00:14,510 --> 00:00:19,025 The response object is a data event that gets emitted when 7 00:00:19,025 --> 00:00:24,140 a piece of data like student data from an API comes in. 8 00:00:24,140 --> 00:00:26,850 Let's check out what happens when this event occurs in our code. 9 00:00:28,120 --> 00:00:30,248 We can add a handler for the data event and 10 00:00:30,248 --> 00:00:34,310 give it a console.dir statement to print out the response. 11 00:00:34,310 --> 00:00:38,060 In Node, we handle events using the on method. 12 00:00:38,060 --> 00:00:42,019 We pass the on method two arguments, an event to watch for and 13 00:00:42,019 --> 00:00:45,890 a callback function to run when that event occurs. 14 00:00:45,890 --> 00:00:48,040 Similar to addEventListener. 15 00:00:48,040 --> 00:00:50,890 In this case, the event to watch for is the data event. 16 00:00:51,900 --> 00:00:55,950 The data event occurs when data is received by our program. 17 00:00:55,950 --> 00:01:00,603 Passing around large amounts of data is best done with a certain type of data. 18 00:01:00,603 --> 00:01:01,253 Buffers. 19 00:01:04,280 --> 00:01:08,544 All data types like strings are useful for humans, machines use binary data or 20 00:01:08,544 --> 00:01:11,320 data in the forms of ones and zeros. 21 00:01:11,320 --> 00:01:15,660 Large amounts of binary data are called streams and can be stored using buffers. 22 00:01:16,690 --> 00:01:21,083 We'll add a console.dir statement to get a better idea of what buffers look like. 23 00:01:22,339 --> 00:01:25,079 When we run our code, something interesting should happen. 24 00:01:33,030 --> 00:01:36,115 So first watch for the data event on the response object. 25 00:01:42,800 --> 00:01:46,732 And we'll pass the data in to the callback function, 26 00:01:53,744 --> 00:02:00,988 console.dir(data); Back to 27 00:02:00,988 --> 00:02:07,380 the terminal, there's our buffers. 28 00:02:08,810 --> 00:02:13,920 Now, this data isn't really readable or useful for us in this format. 29 00:02:13,920 --> 00:02:15,697 But don't worry, we'll fix that. 30 00:02:17,132 --> 00:02:21,971 Because the data comes in piece by piece, we can define a variable to store 31 00:02:21,971 --> 00:02:26,591 the response and concatenate data to it until the stream is complete. 32 00:02:30,019 --> 00:02:33,151 I'll create a variable named body to store the response body and 33 00:02:33,151 --> 00:02:34,781 set it to an empty string for now. 34 00:02:36,740 --> 00:02:42,010 Inside this event handler, instead of printing out the response, 35 00:02:43,707 --> 00:02:48,938 Let's add the data. 36 00:02:53,630 --> 00:02:55,710 We know this data is in the form of a buffer. 37 00:02:56,810 --> 00:03:01,691 We'll want to convert that to a string, so that we can use it. 38 00:03:04,769 --> 00:03:06,938 How will we know when the request has ended? 39 00:03:09,189 --> 00:03:13,864 Along with the data event, there's also an end event that's emitted 40 00:03:13,864 --> 00:03:17,680 when there is no more data to be consumed from the stream. 41 00:03:18,750 --> 00:03:21,080 This tells us when all the data has been sent. 42 00:03:21,080 --> 00:03:24,759 And we can handle this event just like other events in our callback. 43 00:03:30,459 --> 00:03:34,670 So on the response object, we'll watch for the end event. 44 00:03:36,420 --> 00:03:40,890 And when this happens, we'll run a callback function, 45 00:03:47,889 --> 00:03:55,274 Print out the body object or the body string in this case, save. 46 00:04:01,679 --> 00:04:04,949 And our entire response body is returned. 47 00:04:04,949 --> 00:04:09,510 Now in this format, the data isn't super readable or usable. 48 00:04:10,620 --> 00:04:14,820 We'll fix that, we wanna make this string an object so it's easier to interact with. 49 00:04:16,010 --> 00:04:19,740 As an object, we can access only the properties we want. 50 00:04:19,740 --> 00:04:23,100 Converting a string to a data structure is called parsing. 51 00:04:23,100 --> 00:04:25,187 JSON is a native JavaScript object, so 52 00:04:25,187 --> 00:04:28,430 we can use one of its methods to parse our string. 53 00:04:28,430 --> 00:04:33,379 The JSON parse method takes a string and returns a JSON object. 54 00:04:41,575 --> 00:04:48,623 We'll run JSON.parse on our body, Save. 55 00:04:50,013 --> 00:04:51,663 Now try printing this out. 56 00:05:00,062 --> 00:05:03,349 Now our data is an object and it's much more readable. 57 00:05:04,808 --> 00:05:09,300 We get an array with all of Carlos's badges and point totals. 58 00:05:10,730 --> 00:05:14,291 In the next video we'll wrap up our project's main functionality and 59 00:05:14,291 --> 00:05:16,800 add the ability to search for different users.