Complete Intro to Linux and the Command-Line curl Basics
Transcript from the "curl Basics" Lesson
>> Okay, so we just went over Wget, which is a fine and great program, but I wanna get you into cURL, which is actually what I use most of the time. I find like cURL fits better into kind of like the greater Unix ecosystem, so I think it's easier to use.
[00:00:13] And easier to use, I'm not gonna say even easier to use, it fits better my use case for what that program needs to do. So everything that I've showed you with Wget, you can do with cURL. In fact, let's just go ahead and take a look at how you would do that, right?
[00:00:34] So here, I'm gonna go out and grab this same URL, right, the 2048 game. And I'm gonna come over here and say curl, which is gonna go out to the Internet and grab this file, paste that URL. And if I just run it without anything else, it's going to go get that and it's going to print it out to standard out, which is not what you normally want, right?
[00:00:55] You want it to actually go into a file. So you just do it the normal Linux way and I'm just gonna put an angle bracket, and I'm gonna put that into game.sh, right? Yeah, sorry, you have to put the curl in front, curl, URL, angle bracket into game.sh.
[00:01:21] Right, and so now you can see here I have whatever game was, game.sh. And if I cut game.sh, you can see it's the file, right? So that's how you would do it with using cURL, how you would normally do with Wget, right? So again, personal preference. I see people who use both of them.
[00:01:38] I just kind of forget that Wget exists and always use cURL. And I guess the thing to say is wherever Wget's available, almost always cURL is also available. I think there's one distro of Linux, maybe BusyBox Linux or Alpine or something like that, one of those super minimalistic Alpine or Linux distributions omits cURL and just has Wget.
[00:02:06] But like that's the only case that I've ever run into that, and you can always install cURL very easily. So what I'm saying is you can focus on one and forget about the other, and that's totally okay. Okay, so now what I wanna show you to do is what I primarily use cURL for, I don't frequently download files from the Internet into my shell.
[00:02:29] I mean that does happen maybe once a month, once every other month. But I do a lot of API development since I'm a Node.js developer. And when you're doing API development, you frequently want to hit an endpoint and see if it's responding the way that you want it to, right, and cURL is perfect for that.
[00:02:47] So I'm gonna show you how to do that, you would do this instead of using something like Postman or Insomnia, or something like like that, you can do all this stuff just using good old fashioned cURL. So what we're gonna do is we're gonna start an API server on our local VM, and then we're gonna use cURL to hit it.
[00:03:08] So what I want you to do is I want you to open another tab here. So again, this is gonna drop me into Mac, and I'm going to say multipass shell primary. And again, you can do this with PowerShell to get into it as well. Okay, so now I have two open windows and they're both connected primary, it's not two different VM to this, both are connected to the same VM.
[00:03:34] And we're gonna use Python to start just a super quick server, right? So go to your home directory, right, so I'm in my home directory here. And I'm gonna say python3 -m http.server 8000 dash, dash bind, 0.0.0.0, okay? So now this is going to start running a python3 static web server, right?
[00:04:08] So this would be analogous to something like Nginx, it'd be analogous to Apache, or PHP -S, or Node.js is like HTTP or server, something like that, right? So it's just gonna serve files from your local directory. And we're gonna do this to kind of emulate what an API server would look like.
[00:04:29] So just to show you how this works, if I come over here to the other one, I'm gonna hit ifconfig just to get the same Internet address, right? So this one's 192.168.64.2, I'm going to grab that. I'm gonna come over here and I'm gonna open a new tab just in my local browser, paste that in there and then put colon 8000.
[00:04:53] And you can see here this is running a local server from that home directory, so you can see these are all the files that I have in my home directory. Mind you, don't necessarily run a server like this is, this is just purely for development purposes. But the reason why I'm doing this with you right now, if we look back over our server, it's going to show you all the traffic that it's gonna be getting, right?
[00:05:15] So it means someone from this IP address, my IP address, at this time made a GET request to the root directory using HTTP 1.1 and it gave back a 200 response, right? So we're gonna be doing some cURL requests to show you how this is going to be hitting the server.
[00:05:35] Makes sense so far? So I'm showing you with Python just because Python I know was installed on every Ubuntu box. Well, it's like we would have to go install Node to use Node, you do not wanna learn Python from me. There's still Reddit code in production that runs my Python, and I'm surprised that it's still standing up if we're being totally honest about it.
[00:05:56] I wrote Python for like six months and so it was okay, it was an interesting time. Most of Python I learned from Nina Zakharenko, who has a Python course now in Frontend Masters. We used to work together at Reddit.