This course has been updated! We now recommend you take the Introduction to Node.js, v3 course.

Check out a free preview of the full Introduction to Node.js, v2 course:
The "Testing an API with HTTPie" Lesson is part of the full, Introduction to Node.js, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott uses the HTTPie command line tool to interact with the Node.js server. HTTPie allows requests to be sent to a server endpoint and displays the response. Questions about other CLI frameworks are also covered in this segment.

Get Unlimited Access Now

Transcript from the "Testing an API with HTTPie" Lesson

[00:00:00]
>> I have something on the machine called HTTPie, I use this all the time, I talk about a lot of my courses. HTTPie it's basically a theory ever heard of curl is like curl, but easier to use. You can install it if you want, you don't have to use this, you can use whatever you want.

[00:00:22] You got postman, you got whatever API client you want to interact with the API's. But I do recommend getting something comfortable whether it's a GUI, postman, whether it's a terminal thing like HTTPie, whatever. Finding your tool to interact with API's is going to be helpful because if you're developing on the back end, you're developing API's, you need to be able to test them.

[00:00:41] We'll just execute them real quick, and see what's happening. Yeah, so highly recommend doing that. I'm gonna use HTTPie to do that and show you how that works. So we're on localhost 8000. So I know what HTTPie, I can say HTTP If I'm gonna be on localhost, I can just admit the hostname is for the port which is 8000.

[00:01:01] And if I don't put anything else, it'll just do a get request to the blank URL. So let's try that, somebody will get requests. And you can see I get Hi. And that's exactly what I thought was gonna happen because it wasn't a POST request. So went down here to this else, it did a 200 and it said.

[00:01:18] Hi, so if I go back and look, I get it, okay. 200 means okay. And I get a hi. That's exactly what I thought. So let's try to do a post now. So I'm gonna say HTTP and we'll say post 8000. And then I'm gonna post some JSON.

[00:01:34] So I'm gonna say color= red and then food=pizza. So this is basically creating an object with a color field and a food fields. That's what's this is doing. We'll post that and then I get okay, right? And I get this 200 it says it was created, but if I go back and look at the logs, I'm pretty sure I log this or go back over here you can see there's an object we set up.

[00:02:02] And that was after we chunked it all together right? Cuz if we go back and look at our code, I had to chunk this together as in that JSON object that was sent up from HTTPie was broken up into little bits of data. And every time one of them came in, I appended it to this body string.

[00:02:25] And then after it was all said and done, I was able to log it which gave me that JSON object, right so if I wanna turn this back into the actual object X to JSON dot parse this body, so we can get some nice color output going. So let's stop this started again.

[00:02:39] Run that again. And then boom, you can see it's an actual JSON object now, and not just a JSON string. It's an object. And that's the difference. So you can see that's, I mean, yeah, that's pretty cool. But who wants to do this? I don't wanna do this.

[00:02:54] I don't wanna set all this stuff up and sure this is cool if I'm just doing one little well, it's a post or not, what if I have multiple routes, and sub routes, and dynamic paths, and different things like that. It can get pretty complicated. I mean, you can imagine all the if statements in here, I'm talking about, well, if it's a method post, and if the URL contains this, and it has that would be crazy, especially if you're building API based products I can't imagine.

[00:03:18] So there are tools for this. And I'm gonna walk you through how to use those tools and those packages. But before I do that, any questions on creating a low level server and Node JS? Do I have any COI frameworks that I recommend the top of my head?

[00:03:34] I think I can think of two one is Caporal. Caporal, CLI and we'll type that in. Yep, this one is pretty good. I've used this one before, you can kind of see the syntax, how different that is. So you literally get the program, you give it a version.

[00:03:55] You make a command, you define some arguments, some options, and then you run the callback when someone calls that command. It's literally that simple. And it creates a nice help script for you when someone calls help, it collects all your commands and prints them out for you. It's really cool.

[00:04:09] So this one's really good. I think the standard that a lot people use this commander. Yeah commander. A lot of people still use this one. I think this was the de facto one back in the day it is still might be. I forgot why I stopped using it but it is pretty good.

[00:04:27] You can see it's very similar. Everyone's pretty much got their opinions based off this one. This is like one of the first ones created by some guy named TJ Holloway Chuck that you probably will never hear up now but it was pretty much a legend. In the node industry back in the day, and still this dude's amazing, but pretty sure he created this.

[00:04:43] So yeah, that these two are pretty good. And I think there's another one from Heroku, Heroku CLI framework, For the name of it. Its called oclif, yeah, I've tried this before, but I don't have a lot of experience with it, but I've heard good things. So oclif is another one that you can check out.

[00:05:06] But yeah, those are probably this is the one that I can think of off the top of my head. So yeah. Good question. Anything else? Yeah, so the question is, do I still build servers with node JS? Yes, I do. Because, There's something not, okay, how to describe this?

[00:05:26] It's not that there aren't better tools there are. But sometimes you need a little more flexibility on what you're doing. And just having a nice open server is great for that. Sometimes I'll just go serverless and I'll just use something functions and next as, but maybe I don't need a front end I just need some API routes.

[00:05:44] And I need to keep it long running because of the type of database that I'm connected to. If you ever tried to connect to a Mongo database in the service environment, it's hair pulling. It's ridiculous cuz it's not meant to be installed in an environment that's gonna be shutting down all the time.

[00:06:01] It's meant to be open the whole time with its pool, his connections being pulled and everything like that. So sometimes the servers is a little more convenient. And then also, if I'm doing some intensive stuff, I want a server. So it really just depends. But it's never at the point where it's well, it's either this or it's that.

[00:06:20] It really just depends on what you're making and what is gonna be best for your job, I wouldn't build a crawler index JS, I would probably put that in node or, maybe a service function, but I wouldn't added index JS along an app. And also, if my product was an API, if I was a SAS company, a developer tools company and my product was an API.

[00:06:43] I probably I wouldn't have that product be sitting next to nextjs server functions. It would be its own thing behind the gateway, behind a proxy, behind all this other stuff. And the only way you're gonna get there is with an actual API and actual server. So yeah, cuz sometimes you need access to those things.

[00:07:03] So it really just depends on what you're doing. But I would say for most cases, if you're just, I just need something to send back, JSON. Yeah, you can get away with that without having to build a full server. But if you're asking that because this seemed kinda scary, well wait till I show you how to do it the easy way.

[00:07:18] And then maybe you won't see some scary. One more question, okay.
>> Yeah, I don't know if you can speak at a high level about node as a back end versus Java or Ruby.
>> Yeah, I can. My current job we have a lot of backends and a lot of different languages.

[00:07:36] So I've had some experience with that. And actually, the first language that I ever learned was Ruby that I learned Python. And then I did learn Java, but not for servers, but for android so I don't really have any server experience there other than my current job now which does use a lot of Java.

[00:07:52] Yes, I would say, I would always choose node high-level when I don't have any, high CPU intensive tasks. So things like arithmetic, A.I ,machine learning. Yes, you can do it a node absolutely. But it wasn't meant for that. And if you tried to do that it's like trying to squeeze a size 15 shoe in a size 15 foot in a size 10 shoe is just it's gonna hurt.

[00:08:15] But you could probably do it. So you better off using node for things like I need a JSON API, I need to query your database and they send back some data, that's it. It's a crud API, that's all that I'm doing. It's not doing crazy stuff. That's what you would use node for, cuz it's able to handle a lot of requests out of the box without any tweaking.

[00:08:35] That's what it was built for. So if you're going for more request per second, and you're going for the low CPU intensive tasks, and JavaScript, node is a really good choice. In fact, the thing is you don't really need to unless you're a billion dollar company where a fraction of performance could cost you hundreds of thousands if not millions of dollars.

[00:08:56] Doesn't really matter what language you pick, pick the one that works the best for you. It's gonna get the job done. No one's gonna not use your app because you decide to use node. Mostly we don't even know what node is, especially your customers who probably aren't technical.

[00:09:09] So pick the thing that allows you to get done faster because at the end of the day, it's about being productive and not about what framework or tools you use until it actually does become a problem. If you're literally Dropbox, well, we can't literally use this language because we need this one feature and it's costing us money and, okay, then yeah, you should probably go figure that out.

[00:09:28] But most people aren't in that position. So high level for me, it's always just come down to, well, I know JavaScript the best, so I always choose node. That's it. It can do everything that anything else can do up until I'm making a billion dollars. And I haven't made a billion dollars yet, so I'm gonna keep using node probably