In this article, we will learn how to use the Curl command to access the URL of a website. So by doing this, we will be able to fetch a web page from the website. Or learn how to download an image from a given URL.
But before we take a look at that, we want to make sure you are aware of what a Curl command line tool is and how it works. So if you do not already know about of it, then take a look at our earlier tutorial:
But if you are already aware of how curl works, then it is about time we take a look at how to use it to access an URL.
How To Use Curl Command To Access An URL
Curl, as we had discussed in our earlier article linked above, can understand a lot of protocols. These includes FTP, SMTP, POP3, MQTT etc.
But the most used protocol among them all are the HTTP and HTTPS protocols. But why, you may ask, right?
It is because you can use these protocols to talk to a web server. So, you can use HTTP from curl to request for a web page or an image file or anything else. So any resource that has an URL, can be accessed using the curl command!
Is that clear? Great! So then we will now take a look at how to do that in the next section.
Example Curl Command To Access URL
So let us say that you want to access Google’s home page from command line using curl. How do you do that? Take a look at the code below:
curl -X GET https://www.google.com
This is the curl command you need to use to fetch the Google’s home page. Does it look a bit daunting? Well, do not worry. Because I will explain to you what each of these part of the command is actually doing.
So let us take a look at the command, one part at a time, alright?
“curl” – This is the first part of the command, which as you know will call the curl command line tool.
“- X GET” – What is this, you may wonder right? Well, curl as you know supports a lot of protocols. And each of these protocols have their own set of options built into it. So in case of HTTP, you know that we can send many types of requests to a web server. Some of the prominent ones include GET, POST, PUT, DELETE etc. So in case of curl, we can send a GET request to a web server using the -X GET switch option!
“https://www.google.com” – Finally, this is the web server URL to which we want to send the GET request.
So that is it! That is all you need to enter as part of the curl command to access an URL of a website.
We suggest you practice using this curl command a few more times to get used to it. Once you do that, you will get a clear understanding of how Curl works under the hood.
If you have any questions about it, do let me know in the comment below and I will be happy to answer, alright?
So see you until next time! 🙂