Linux/Mac Terminal Tutorial: How To Use The cURL Command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody how's it going in this video we're going to be learning how to use the curl command so the curl command allows us to query URLs from the command line so not only can we make regular requests but we can post form data authenticate users save responses to files on our system and all kinds of useful stuff so I use this command a lot when I'm testing REST API s and things like that so let's go ahead and take a look at some examples so first let's just run a simple curl command so to do this we can just type in curl and then the website so I'm going to run curl on my personal website here so if I go ahead and run this then you can see that the response that we get back in the command line is all of the HTML and scripts and everything that is usually rendered by the browser but this is just all of it in text form now that's kind of a lot of text to work with so to simplify things a bit I have a sample website running on my local web server that I'm going to use for most of this tutorial so if instead I run a curl command on my localhost and that's going to be running on port 5,000 so now you can see that we get a small response that says that we've hit the test home page okay so now we're getting an HTML response from a URL but now let's see what else we can do so one thing I mentioned was that I used curl to test REST API s so let's go to a route that will return a JSON response and see what that looks like so I have a route set up on this local server that will return some JSON and that is just at test JSON actually that route is JSON test okay so you can see that we got a small sample JSON response here within the terminal so now let's say that we wanted to see the response header along with the content so to do this we can run the same command but I'm going to add a dash I here at the beginning now we could also use a dash dash include but the dash I is for short so if I go ahead and run this so now you can see that we got some extra information here with the response header so we can see that we got a 200 response and that the content is JSON and we can also see some server information and things like that okay so now what if instead of just getting information we wanted to post update or delete information now this is also pretty common when working with REST API or other websites so curl has the ability to use different HTTP methods when requesting a URL so I have a simple route here that will change its output based on the request that it receives so for example if I just do a curl on this methods route and I'm going to go ahead and remove that - I you can see that the response that we got back was that we sent a get request so now let's say that we wanted to simulate submitting form data to the same URL now if you're familiar with HTTP methods then this would be a post request so in order to post some data to this URL we can use the - D option now we could also use - - data but the - D is the shorthand for that so now let's go ahead and what data do we want to post so this is kind of like how we would put parameters into a URL so I'm going to say first equals Corey and last equals Schaefer and let's go ahead and send that so now you can see when I post data to that route that it says that we sent a post request and it also gives us the information that we posted so the post method is used by default when we send in data but we could also be wanting to update the data so we can do that by using the put method so to do this we have to specifically specify put and we can do that by at the beginning here we can say - capital X that's a capital X and then we can just say put and so now for example let's say that I wanted to update this to Cory Smith instead of Corey Schaeffer so now if I run that you can see that it responds back and says that we sent a put request with the information that we sent in and lastly if we wanted to do a elite then we could come in here and just remove this data and we could just send in a delete instead and you can see that it catches that we sent that delete request okay so what else can we do with the curl command so sometimes you'll run into routes that require authentication so for example here I have a test route that requires a username and password and if I try to run curl command on that route without requiring a username and password and that is just here at this secret route then you can see that it says that it couldn't verify our access and that we have to provide a valid username and password so to provide a username and password we can use the - you option and after that - you then we can pass in the username and password and for this route my user name is kori and my password is passed so first we pass in the username and we separate that with a colon and then the password so now if I do that and post that to the route then you can see that it says that we were successfully authenticated ok so another thing that people commonly need to do with curl is that they need to be able to download the response from a route so I have a download route here which sends a picture file now if I just curl the download route without any other options then you can see that we get back a bunch of gibberish and that's because we're seeing the binary file represented in text so to actually download this file we can use the - Oh option so if I do - oh now we could also do - - output but - oh is the shorthand for that so I'm just going to call this test dot jpg so we're saying that we want to download the response of that route so if I go ahead and do that then you can see that it pulls up the information here of our average download speed and things like that so now if I look here in my current directory then you can see that we did download that image and if I open that image then you can see that we did download that successfully from the route now saving your responses isn't only useful for file downloads now I use this all the time when I want to save a large JSON response directly from a route so for example here if I go down to Google and pull up this web page here I have github public API pulled up here on my browser and I've navigated to a route that returns JSON information of the commits that I've made to this demo repository so instead of trying to copy and paste this into a file manually let's just download this using curl so what I'm going to do is I'm going to come up here and copy this route and then pull back open my terminal so to do this we would just do curl and then - OH - save the output and I'm just going to call this commits JSON and then paste in that route so now if we do an LS on this directory you can see that we have this commits JSON and if I open this up then you can see that we did get all that information saved so now we have it saved for later use if we want to do any analysis or anything like that so it's really useful okay so I think that is going to do it for this video I hope that this introduction to the curl command gives you an idea for how useful of the tool it can be but really we only scratch the surface of what all it can do so it can be used to transfer files pass cookies test connection speeds and all kinds of useful stuff so hopefully after this video you'll have a few ideas for how you can begin using curl from day to day but if you do have any questions and feel free to ask in the comment section below and I'll do my best to answer those if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest way is to simply like the video and give it a thumbs up and also it's a huge help to share these videos with others who you think would find them useful and if you have the means you can contribute through patreon and there's a link to that page in the description section below be sure to subscribe for future videos and thank you all for watching you
Info
Channel: Corey Schafer
Views: 141,962
Rating: undefined out of 5
Keywords: curl, cURL, Terminal, Linux, Mac, Linux Terminal, Command Line, Command-Line, Linux Command Line, Linux Commands, Mac Commands, Unix, Darwin, CLI, Command Line Interface, How to use, Tutorial, SysAdmin, REST Service, API, Sys Admin, How-to
Id: WxUVU0b95Oc
Channel Id: undefined
Length: 8min 57sec (537 seconds)
Published: Thu Jun 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.