introduction
In modern network development and operation and maintenance work, curl command is an indispensable tool. It is a file transfer tool that uses URL syntax to work under the command line, and supports multiple protocols, such as HTTP, HTTPS, FTP, etc. Through the flexible combination of various parameters, curl can realize rich network request functions, helping us perform interface testing, data crawling, network debugging and other operations.
1. Basic request parameters
1. -X or --request
This parameter is used to specify the request method, such as GET, POST, PUT, DELETE, etc. By default,curl
Use the GET request method. For example:
curl -X POST /api/data
The above command/api/data
Send a POST request.
2. -d or --data
Used to send data to the server, often used in conjunction with POST requests. The data can be in the form of a key-value pair, or in the JSON format, etc. For example:
curl -X POST -d "name=Kimi&age=25" /api/user
This command sends a POST request to the server containing form data, and the request body containsname
andage
Two fields.
3. -H or --header
Used to add custom request headers. When interacting with the server, you sometimes need to set specific request headers, such asContent-Type
、Authorization
wait. For example:
curl -H "Content-Type: application/json" -d '{"name":"Kimi","age":25}' /api/user
A request header is set hereContent-Type
forapplication/json
, and send data in JSON format.
2. Output control parameters
1. -i or --include
Include HTTP response headers in the output. This is useful for debugging and viewing the complete information returned by the server. For example:
curl -i
After execution, not only the web page content will be displayed, but also the HTTP response status code, response header and other information will be displayed.
2. -o or --output
Save the output to the specified file. For example:
curl -o
This command willSave the content to local
in the file.
3. -s or --silent
Silent mode, no error and progress information is output, only the server's response content is output. This is for use in scriptscurl
When avoiding output interference is very helpful. For example:
curl -s
3. Connection and transmission parameters
1.-L or --location
Automatically handle server redirection. When the server returns a redirect response such as 301, 302, etc.curl
The request will be automatically followed by the redirected URL. For example:
curl -L /old-page
If /old-page is redirected to /new-page, curl will automatically request a new URL.
2. -C or --continue-at
The breakpoint continues to pass. When downloading large files, if the connection is interrupted, you can use this parameter to continue downloading from the location where the last interrupted is. For example:
curl -C - -o /
If the file has been downloaded before and is interrupted,curl
The download will continue from the location where it was last downloaded.
3. -T or --upload-file
Used to upload files. You can upload local files to a location specified by the server. For example:
curl -T ftp:///upload
Upload the local file to the upload directory of the FTP server.
4. SSL/TLS related parameters
1. -k or --insecure
Allow curl to interact with HTTPS servers that use self-signed certificates or certificate chains incomplete, ignoring certificate verification errors. This parameter is more useful in a test environment or in scenarios where certificate security requirements are not high. For example:
curl -k
However, it should be noted that using this parameter will reduce security because the identity of the server cannot be verified.
2. -E or --cert
Specify the client certificate file. This parameter is required when interacting with an HTTPS server that requires client certificate verification. For example:
curl -E
hereIt is a client certificate file.
3. --cacert
Specify the CA certificate file. Used to verify the validity of the server certificate. For example:
curl --cacert
It is a CA certificate file, which uses it to verify that the server certificate is issued by a trusted CA.
V. Performance and debugging parameters
1. -w or --write-out
Custom output format, used to output relevant information about requests and responses, such as time-consuming, status codes, etc. This is very helpful for performance testing and debugging. For example:
curl -w "status_code: %{http_code}, time_total: %{time_total}"
After execution, the HTTP status code and the total time-consuming request will be output.
2. -v or --verbose
Enable detailed mode, outputcurl
Detailed information about interacting with the server, including request headers, response headers, SSL handshake information, etc. This is very useful for debugging network issues and viewing request processes. For example:
curl -v
After execution, a detailed request and response process will be displayed.
6. Practical application cases
1. Interface testing
Suppose we want to test a RESTful API that provides query and update functionality for user information. First, we can usecurl
Send a GET request to query user information:
curl -X GET /users/1
Then, use POST request to update user information:
curl -X POST -H "Content-Type: application/json" -d '{"name":"Kimi_updated","age":26}' /users/1
By viewing the returned status code and response content, you can determine whether the interface is working normally.
2. Network packet capture and debugging
When we encounter a network request exception, we can usecurl -v
To crawl the detailed information of the request and response. For example, a request always returns a 404 error, which we can execute:
curl -v /problematic-page
By viewing the detailed information of the output, we can check whether the request header is correct, whether the path is correct, the response header returned by the server, etc., thereby positioning the problem.
3. File download and upload
usecurl
Download the file:
curl -o /files/
Upload files to the FTP server:
curl -T ftp:///upload --user username:password
Also used here--user
Parameters to specify the username and password of the FTP server.
This is the article about the detailed explanation of the practical application of Curl parameters in Linux. For more related Linux Curl parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!