Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
We have a working REST API, let's go poke it with a stick. Let me show off one of my favorite REST tools Postman.
Installation Instructions
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, so let's start out with a little
manual test to see if it's working.
0:00
Now, we could definitely write up
some JavaScript to test it out, but
0:04
there are better ways.
0:07
A super handy Chrome web app that I use
to test my rest APIs is called Postman.
0:09
Installation instructions
are in the teacher's notes.
0:14
Let me show you how to test your APIs.
0:17
All right, so I was reviewing
the code that we just wrote and
0:20
I found a couple mistakes
in the same line.
0:22
It happens, totally happens,
these are mistakes, we all make them.
0:25
At the end of this extremely
long line that's right here,
0:29
the very end of this, I forgot to
add the final closing single quote.
0:33
Maybe you saw that when I did that.
0:38
The other thing that I forgot to do is
that there's extra parameters here.
0:40
So there's username and password.
0:43
And we're not using them, but
if we don't put them there,
0:46
we have a bit of a problem.
0:48
It's a different method signature, and
0:52
we're gonna end up
calling the wrong thing.
0:54
So that is now set up correctly.
0:56
Okay, so
we're ready to get the server running.
0:59
So let's choose Run, and
we'll choose Run, and
1:00
this will create a new configuration for
us called API, so we'll click that.
1:04
There we go.
It says Spark has ignited.
1:08
All right, now, I'm gonna go and
open up my Postman,
1:10
which you probably have installed.
1:14
We might be running different versions,
so don't let that freak you out.
1:15
It's an awesome tool.
1:17
They're always adding new stuff.
1:18
So the first thing that I want
you to know here about Postman
1:20
is that you can do the different
types of requests.
1:23
So what we wanna do first is we wanna
post so that we can add a course.
1:26
So we're gonna do a post,
and we're gonna post to
1:29
localhost4567, and
we're gonna post to courses.
1:32
I'm going to put an HTTP
at the front of that.
1:39
So we're gonna post to the URI of courses,
and this is our server, right?
1:43
It starts on 4567 as the default port.
1:48
So, then why don't we add a header,
1:51
a specific header that says content type,
right?
1:53
Because remember, it's only listening to
that and I accidentally sent the post.
1:57
So, content type and
the value here is application/json.
2:04
Okay.
2:10
In the body,
we also wanna pass the json, right?
2:12
Remember, that's what we have promised,
we had said we're going to send
2:15
application json, so
we're gonna right some json here.
2:19
So, we'll do the name that we wanna send.
2:21
So json is a key value and key colon,
then the value that you want.
2:25
So we'll say REST API Basics and
then the URL,
2:31
and then again, so
if the URL's the key and the value is
2:37
http:teamtreehouse.com/Library/rest-api-b-
asics, cool.
2:43
All right, so
that's the body that we're gonna send up.
2:52
So let's go ahead.
2:54
So we're gonna post to courses,
saying that we are application json and
2:54
that's what the body looks like.
2:59
So let's click Send.
3:00
And we can scroll to the response.
3:02
And there it is.
3:04
There is REST API Basics.
3:05
You probably have id 1.
3:07
I had just tested this before
to make sure that it works.
3:08
So it has the id back.
3:11
So that's awesome.
3:12
And also notice here
that is says 201 Created.
3:13
That's because we sent that status code
back, cuz we're good API citizens.
3:16
Isn't that awesome?
3:19
And so now,
what happens is it stores that over here.
3:20
So these are the ones that I did
accidentally when I pressed Enter and
3:26
here is the good working
one that we passed.
3:29
So we can make a new request.
3:31
So let's make a different one,
so let's do name,
3:33
let make this will be Java basics,
remember that one?
3:36
And we'll change this
from rest api to be java.
3:40
And we'll click Send.
3:46
Okay, we'll scroll to the response,
and boom, there's a new one.
3:49
It's got id number 3, awesome.
3:51
So, post works, and now there's two, so
let's switch our request to be a GET,
3:53
right?
3:58
So now we can list all of
the courses that are available.
3:58
So let's go ahead and send that GET.
4:02
And that REST API Basics,
that was the one that I tested with.
4:06
Here's the two that we added, and
on yours there should just be two, right?
4:08
And see how there's these
hard brackets here.
4:12
See how it's showing that there is
an array of these different courses.
4:13
Awesome!
4:18
And we also added a row that
lets you see different IDs.
4:19
So if I said 2,
it should just return the 2.
4:23
Awesome, it does.
4:25
And if I say 3, does it get the third one?
4:26
Awesome.
4:29
And what happens if we do a different
idea, like what if we do 42?
4:30
Null.
4:34
Hm?
4:35
That's not great, is it?
4:35
Because it says 200 OK but
it's returning a null.
4:38
It really should return a 404 not found,
right?
4:41
Let's table that for now.
4:44
So, we've done our automated testing,
but this is manual testing, right?
4:45
So we've done automated testing
together before where we test
4:50
isolated code in small units of work.
4:53
That's unit testing.
4:55
We can take another approach,
which is called functional testing.
4:56
What this does is it allows you to test
the system more in a way than an end user
4:59
using your system might do things.
5:03
So let's dive into functional testing,
right after this quick break.
5:05
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up