Testing Requests5:48 with Alena Holligan
To test our API, we're going to be using Postman. This will allow us to easily format our requests and view details about the response. We can use postman to easily view all the get routes we just set up. We can also look deeper into the response to see the status code and content type being returned. We're going to use postman to create, update and delete our records.
To test our API, we're going to be using Postman. 0:00 This will allow us to easily format our request and 0:04 view details about the response. 0:08 Check the note for more information on Postman. 0:10 We can use Postman to easily view all the getRoutes we just set up. 0:13 We can also take a deeper look into the response to see the status code. 0:21 And in headers, we can see the content type. 0:28 We're going to use Postman to create, update, and delete our records. 0:33 Let's go back into our application to start setting up those routes. 0:38 I'm going to duplicate this second route that gets a single course. 0:43 To create a record, the route will use the post action. 0:49 This post action will post directly to our course's collection, so 0:57 we don't want anything appended here. 1:02 This time we want to call the createCourse method. 1:05 And for post, the details will be found in our request->getParsedBody. 1:12 I'm going to change the status code to 201 to signify that a record has been created. 1:23 Let's try creating a record in Postman. 1:32 We're going to post to our courses with JSON as the body. 1:40 Headers application/json, and Send. 1:48 We can see our new course that we've added and 1:56 if we go back to All Courses, 2:01 we can see that we now have four courses. 2:04 We can also post with a form. 2:10 Our headers, application/x-www-form-urlencoded, 2:15 and in our body you'll see the url encoded with our title, and our url. 2:20 Once again, we see our course created. 2:32 And now we have five courses. 2:37 Let's go back and update these records. 2:40 I'm going to duplicate the post, and 2:47 then to update we're going to use the put action. 2:50 Well we could include the course ID in the body of the request, 2:54 it's more appropriate to add that to the requested url. 2:58 I'm going to append/course_id. 3:02 And then, I'm going to create the data. 3:07 I'm going to first set this equal to the getParsedBody. 3:11 And then I'm going to add ['course_id'] = $args['course_id']. 3:17 And then we'll pass the data to updateCourse. 3:29 Now let's update the record in Postman. 3:36 We're going to put to our courses.4 our updated record. 3:44 And we see our status and the results. 3:55 If we get all courses again, 3:59 we see that our fourth course id has updated the title and url. 4:01 For our final action, let's delete a course. 4:07 I'm going to copy this getRoute, and 4:10 then for our action, we specify delete. 4:15 Our url will specify the course_id and 4:21 we'll deleteCourseMethod, passing the course_id. 4:25 Let's go back to postman. 4:31 Our option is delete and we have our courses/5. 4:39 So this one here should delete. 4:46 Tells us the course was deleted. 4:51 And then if we getCourses, our course is still there. 4:55 It didn't actually delete. 4:59 Let's go back to our code. 5:00 Let's go into our course and find our delete. 5:02 Let's see, we're accepting in the course_id. 5:05 'DELETE FROM courses WHERE id=: id'. 5:08 Then in our bindParam, we're passing data. 5:12 We're not using data, we're just using course_id. 5:16 Let's try this again. 5:20 Tells us the course was deleted, And our course was actually deleted. 5:23 That showed us that our API could still use some more error control. 5:30 But before we get to that, let's add routes for our reviews as well. 5:35 Why don't you see if you can implement them yourself, and 5:41 I'll show you what I came up with in the next step. 5:44
You need to sign up for Treehouse in order to download course files.Sign up