Basic cURL Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys if you've been following me for a while then you know that occasionally I like to take a technology or a tool for web development and programming and just touch on the basics there's a lot of info out there but often times it's explained in a much more complicated way than it really needs to be so in this video we're gonna take a look at curl or see URL curl is a tool for for transferring data to and from a server and for making various types of requests it's a great tool for testing rest api's downloading files and so on many developers would prefer to use something like postman to test api's but curl is another option and it also supports just about every protocol under the Sun you can see here it supports HTTP HTTP FTP IMAP and pop3 SMTP so it supports all these different protocols alright in this video we're going to be working with HTTP and FTP so as far as your environment in your operating system modern versions of OS X and most Linux distributions have curl and Lib curl installed by default if you're using Windows there's a few different things you can do you can download it from here curl dot hacks SC but what I would recommend is to use git bash okay so this is git for Windows it's that git - SCM com I would suggest downloading and installing that if you don't have it already it gives you it gives you a whole bunch of UNIX based commands that you can use including curl it's also just a better command line than the standard windows or even powershell so I would suggest that that's what we're gonna do so I'm just gonna open that up we'll say git bash ok and this is it right here and if we say curl - - help it'll show us all the different options that you can use with your curl commands is just a ton ok we're just we're just scratching the surface today with using some working with the HTTP protocol as well as FTP all right now this is the site that we're going to be working with is Jason placeholder dot type ecoute calm and this is basically a fake online rest API that we can use for testing and stuff so if we scroll down here you can see the different routes that we can use so slash posts will give us a hundred items those comments will give us 500 we have albums photos to do's and users and if I click on for instance posts you'll see it actually gives us a hundred posts if we go down to the bottom here you'll see ID 100 all right so that's what we're gonna be working with now if you wanted to just get a single post you could just go / - for instance and that will give you the the post that has the ID of - alright you can also make different types of requests you can make a post request - slash posts a put which is an update patch or a delete request now it's not going to actually delete stuff off the server it's just for testing so what happens is it'll give you a response back ok it's not going to actually update anything in their database or anything like that it's just for testing and you can also install this locally if you want through NPM alright so let's get to it we're gonna go over into the command prompt and just gonna make this a little bigger ok we'll clear that out and the first thing I want to do is simply say curl and then we're gonna grab this I'm not that we want the posts so we'll go right here and just grab this and paste that in and we'll go ahead and run it and notice that it gives us all 100 posts ok that's the response we get now if we wanted to get a single one again we could just do curl and then that URL and then let's say slash 3 run that and it gives us the post with the ID of 3 alright so if you're creating a REST API this is a really good tool to test this stuff out now there's a ton of options I'm not even going to get close to to them all but one option is to use the - I flag here so let's just say - lowercase I and run that and what that does is it includes all the header information okay so things like the content type you can see it's Jason the length what else access control cache stuff some stuff that I don't even understand but that will include the header now if you just want to get the header you could use either - - head I believe yep or you could do you could use the capital I flag so if we go yeah capital I and then that just gives us the head as well most of these options have a dash version and then a dash dash version all right so let's say we want to to make the request and get this get all these posts and put it into a file okay so for that we can use - lowercase o or - - output so what I'll do here is actually it's going to navigate to a different folder I'm gonna go to my test folder and let's see what's in there I've hello text I'm actually going to delete that so I'm gonna do in RM r and then hello dot txt alright so that will delete it now I want to make the request so let's go back to yeah we'll go right right here this is all the posts and then I'm just gonna add on a - lowercase o alright now I also want to specify the name of the file that I want this stuff to go in so let's say test dot txt all right so if we run that and now we take a look we have test dot txt and if I do cat test dot txt you'll see we have all of the posts in that file now we can also download files with curl so if we were to do let's take away the test txt and then use an uppercase o here and that'll download it so if we go ahead and run that you'll see now we have this post file it doesn't have an extension or anything but if we were to say cat posts you can see they're all in there now if we wanted to download let's say an image for instance this one right here let's go ahead and copy the image address and then we'll go over here and say curl - uppercase o and then we'll go ahead and paste that in that image alright now if we look you'll see we have the the PNG image now we can also limit the transfer rate for download let's go back to that command and then we're just gonna add on right here we'll say - - limit - rate and then we'll say 1000 bytes all right and that'll download it with that limit to the the data transfer so let's say we wanted to make a post request to actually add data to the server and like I said this is only uh this is a fake REST API it's not gonna save anything if we make a post request but it will give us a proper response if it goes through so what we're gonna do here is say curl and then - D you can either use - D or - - data and then we want to put some quotes and let's say we want the title to be hello and let's say the body so we'll say and body equals hello world alright so that's the data we want to send then we need the URL which if we go down here you can see you're allowed to make a post request - slash posts so we obviously want that entire URL which is this right here and then we'll go ahead and put that in here and let's run it and you can see it gives us a response with the title the body and the ID is 101 okay because there's a hundred stored here and the ID is auto increment so the next one is gonna be 101 okay I mean it's not gonna get saved if we reload this you're not gonna see it here but it was a successful response now we can also update data with put of making a put request and see if we go back here and look at the routes you can make a put request to post slash and then we ever ID so let's go over here and just bring that back up and we're gonna say post slash three and let's just do the title hello and then we need to add on right here and specify that we want a put request and we also want to use this - capital X flag so if we run that and what happened oh you know what I did that wrong we have to do - X we don't want the data right there that - - data alright so we run that wait that's not right either we do need the D it just has to go after put sorry about that guys so right here will do - D okay so now you can see we took the ID of three and we changed the title - hello okay so that's what it gives us back now to make a delete requests same thing we're gonna go back and let's change for instance we don't need this we don't need the - D all we want is - X and then delete okay and that made a successful delete request and you can see there's nothing we're back we're getting back an empty object alright now oftentimes when you're creating an API you want to have some secured routes so if that's the case then you just need to add in a couple things so let's say curl and then what we would do is - you and then put your username colon and then whatever the password alright and then you want your you know whatever comes next I don't have anything to test with but that's what you would do if you had something where you needed to authenticate all right so next we have the - uppercase L flag and that's going to follow a redirection so give you an example if we say curl HTTP google.com basically we're getting this response that says 301 moved okay the document has moved to WWE google.com same thing if we go on the browser and we say HTTP google.com it redirects us to the WWV version all right now if you want to actually follow that then you have to add the - el flag so let's do that uppercase L and now you can see it gets google.com for us and it gives us all this crap okay from from that URL alright so next thing I want to do is show you how to work with the FTP protocol which is File Transfer Protocol and I have FileZilla installed and I'm connected to my traversing media website in the test directory so what we can do here is we can upload a file through FTP using curl alright so I'm actually going to I'm going to go to that test directory and let's create a new file and we'll just call it hello dot txt and let me just open that up and we'll just say hello world save that and then I want to upload this to my ft to my server using FTP so to do that we'll say curl and we want to put our credentials just like we would as I showed you before we want to use - U and then the FTP account which for me is test at traversée mediacom alright and then we want to put the password colon and then the password I'm deleting this account before I upload the video so don't bother trying and it just goes to a test folder anyway alright and then we just want to add the - t flag and then the name of the file which is hello dot txt and then we're gonna do FTP and it's gonna be FTP dot Travis E media.com alright so that should do it let's go ahead and run it could not resolve oh I forgot the F alright so it should have uploaded let's go over here and refresh and now you can see hello dot txt now we can easily download files as well so what I'll do is delete this hello text and then what we want to do is go and change this capital T to a capital o get rid of this file name here and we'll say FTP traversal media.com slash hello dot txt all right and then if we look you can see that it's been downloaded alright so that's all that's all we're gonna get into now there's a lot more you can do with curl stuff that's a little more advanced maybe we'll visit that later on down the line but hopefully this gives you a little bit of insight as to what it does and maybe you can use it in your and your endeavors alright so if you like this video please leave it a like subscribe all that stuff alright so thanks for watching and I will see you in the next video
Info
Channel: Traversy Media
Views: 337,999
Rating: undefined out of 5
Keywords: cURL, cURL tutorial, cURL basics, cURL development, cURL http, http, ftp, cURL ftp
Id: 7XUibDYw4mc
Channel Id: undefined
Length: 14min 42sec (882 seconds)
Published: Mon May 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.