Curl (command line tool and library) is a powerful data transfer tool that is widely used to get web content, test APIs, upload files, and more. Its flexibility and functionality make it one of the preferred tools for developers and system administrators. This article will explore tips and practices for using curl to get web content, and how to maximize its functionality.
1. Basic usage of Curl
The most basic usage of Curl is to get web content through the command line. For example, to get the content of a web page, you can use the following command:
curl <URL>
This will output the HTML content of the web page to standard output (usually the terminal window), for example:
curl https://example.com
2. Save content to a file
Sometimes, we want to save the content of a web page to a file instead of just displaying it in the terminal. This can be achieved through the `o` parameter:
curl o filename.html <URL>
This will save the HTML content obtained from the URL to the `filename.html` file.
3. Follow redirects
When the web page you request has redirects, curl will not automatically follow them by default. To follow redirects and get the content of the final page, you can add the `L` parameter:
curl L <URL>
This will automatically follow redirects until the final page content is obtained.
4. Send POST request
In addition to getting the content of a GET request, curl can also send a POST request. Specify the HTTP method through the `X` parameter and send the data through the `d` parameter:
curl X POST d "param1=value1¶m2=value2" <URL>
This will send a POST request to the URL and pass the parameters `param1=value1` and `param2=value2`.
5. Custom HTTP headers
Sometimes, you need to include custom HTTP headers in the request. Use the `H` parameter to add header information:
curl H "ContentType: application/json" <URL>
This will send a request to the URL with an HTTP header with `ContentType` of `application/json`.
6. Use authentication
If the webpage requires authentication to access, curl can implement basic HTTP authentication through the `u` parameter:
curl u username:password <URL>
This will use the provided username and password to perform HTTP authentication and access the URL.
7. Use Cookies
Sometimes you need to include cookie information in the request, you can add cookies through the `b` parameter:
curl b "cookie1=value1; cookie2=value2" <URL>
This will include the specified cookie information in the request.
8. Set the timeout
To avoid long waits, you can set the curl timeout. Use the `m` parameter to set the maximum execution time (in seconds):
curl m 30 <URL>
This will set curl to complete the request within 30 seconds, and will be interrupted if the time exceeds.
9. Parallel requests
Using curl, you can easily perform parallel request testing. Enable parallel mode with the `Z` parameter:
curl Z http://example1.com http://example2.com http://example3.com
This will request multiple URLs simultaneously and display the responses for each.
10. Combine with other tools and scripts
Curl can also be combined with other tools and scripts to achieve more complex automation tasks and testing. For example, you can write a bash script to loop the curl command to check the status of web page content or API.
Contact us with email
How to use proxy?
Which countries have static proxies?
How to use proxies in third-party tools?
How long does it take to receive the proxy balance or get my new account activated after the payment?
Do you offer payment refunds?
Please Contact Customer Service by Email
We will reply you via email within 24h