Curl is a command-line tool for transferring data between servers using various protocols, including HTTP, HTTPS, FTP, and more. It's a useful tool for web scraping as well, as it can retrieve or send data across a network.
Below is a step-by-step guide on how to use Curl to perform an HTTP request.
Making an HTTP GET Request
To make a GET request, use the following command:
curl http://example.com
In this example, we're making a GET request to example.com
. By default, curl uses the GET HTTP method.
Making an HTTP POST Request
To make a POST request, use the -d
option to send data. Here's an example:
curl -d "param1=value1¶m2=value2" http://example.com
In this example, we're making a POST request to example.com
with two data parameters.
Sending JSON Data
To send JSON data, you need to set the Content-Type
header to application/json
. Use the -H
option to set headers. Here's an example:
curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" http://example.com
In this example, we're posting JSON data to example.com
.
Passing HTTP Headers
Use the -H
option to pass headers. Here's an example:
curl -H "User-Agent: MyUserAgent" http://example.com
In this example, we're making a GET request to example.com
with a custom User-Agent
header.
Saving Output to a File
Use the -o
option to save the output of a curl command to a file. Here's an example:
curl -o output.html http://example.com
In this example, we're saving the output of example.com
to output.html
.
Verbose Mode
Use the -v
option to enable verbose mode, which shows information about the whole transaction, including server headers. Here's an example:
curl -v http://example.com
In this example, we're enabling verbose mode for a GET request to example.com
.
Curl is a powerful tool with many more options. You can find more information in the curl documentation.