Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

What is Curl?

Curl supports HTTP, HTTPS, SCP, FTP, SFTP, TELNET, FILE, SMTP, POP3, and so on. Curl supports HTTP, HTTPS, SCP, FTP, SFTP, TELNET, FILE, SMTP, POP3, and so on. Using curl, you can perform HTTP/HTTPS requests, upload or download files, and support cookies, user authentication, proxy support, and speed limiting.

Install Curl

You can use the curl command to check whether curl is installed or not: try ‘curl –help’ or ‘curl –manual’ for more information If you do not have curl installed, you can install it using the package manager:

apt install curl  # Ubuntu
#or
yum install curl  # CentOS
Copy the code

Use the Curl

Syntax format

curl [options] [url...]
Copy the code
  • Options: curl parameter options
  • Url: Indicates the url of the remote server

For example, retrieve www.baidu.com:

curl www.baidu.com <! DOCTYPE html> <! - the STATUS OK - > < HTML > < head >, < title > baidu once, you'll know < / title > < / head > < body link =# 0000 cc >... < / body > < / HTML >
Copy the code

The above command displays the source code for the www.baidu.com home page in a terminal window.

– A designated the user-agent

The -a parameter specifies the User Agent header, namely user-agent.

Curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36' http://httpbin.org/getCopy the code

– b sent cookies

The -b parameter is used to send cookies to the server.

curl -b 'BID=1024' http://httpbin.org/get
Copy the code

The above command generates a header Cookie: BID=1024 and sends a Cookie named BID with a value of 1024 to the server.

Curl also supports reading cookies from files, for example:

curl -b cookies.txt www.baidu.com
Copy the code

– c save cookies

The -c parameter writes the Cookie set by the server to a file.

curl -c cookies.txt www.baidu.com
Copy the code

-d Indicates the data body that sends the POST request

The -d parameter is used to send the data body of the POST request.

curl -d 'name=tigeriaf&password=123'-X POST http://httpbin.org/post
#or
curl -d 'name=tigeriaf' -d 'password=123' -X POST http://httpbin.org/post
Copy the code

When -d is used, HTTP requests are automatically coded with the header “Content-Type”: “application/x-www-form-urlencoded”. Note: -d automatically translates requests into POST requests, and -x POST can be omitted.

-h Specifies header information

The -h parameter is used to add the header information of the HTTP request.

curl -d '{"name": "tigeriaf", "password": "123"}' -H 'Content-Type: application/json' http://httpbin.org/post
Copy the code

The header information for the HTTP request is content-Type: application/json, and the json data is sent with the -d argument.

-i Displays header information

The -i parameter displays the header information returned by the server.

curl -i http://httpbin.org
Copy the code

After receiving the response from the server, output the header of the response from the server first, and then empty a line, and then output the source code of the page.

-o/ -o Saves the result of curl

You can save the result of curl with the -o or -o arguments.

Lowercase -o can specify the name of the file to save, i.e., rename:

curl -o get_res.txt http://httpbin.org/get
Copy the code

Uppercase -o saves the file with its original filename:

curl -O http://httpbin.org/get
Copy the code

-x Sets the proxy

Curl supports different types of proxies, including HTTP, HTTPS, and SOCKS. To transfer data through a proxy server, use the -x (–proxy) option followed by the proxy URL.

The following command will use the proxy request on port 192.168.1.32 6666:

The curl -x 192.168.1.32:6666 http://httpbin.org/getCopy the code

-x Specifies the HTTP request method

The -x parameter specifies the HTTP request method.

curl -X POST http://httpbin.org/post
Copy the code

The command above issues a POST request to http://httpbin.org/post.

conclusion

There are only a few common curl options listed here. To learn more about curl, visit the curl documentation.

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!