Home > Article > Operation and Maintenance > Is curl installed by default on linux?
Linux does not install curl by default. Curl is a file transfer tool that uses URLs to work on the command line. Not all Linux systems come with the curl command. For example, the simple version of Linux systems may not exist. Just install it by executing the "yum install curl" command. That’s it.
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
curl command
The curl command in Linux system is a file transfer tool that uses URL to work on the command line, usually used in Linux system access to services and download files.
The curl command supports HTTP, HTTPS, FTP and other protocols, and can be used to simulate service requests and upload and download files.
Is curl installed by default on Linux?
linux does not install curl by default.
Not all systems come with the curl command, and it may not exist for simple Linux systems. In this case, you can use the yum command to install curl, yum install curl
.
After the installation is complete, you can use the curl --version
command to view the version information of curl in the system.
The syntax format of the curl command
The standard usage syntax of the curl command is: curl [options] [url]
, where options
is used to specify parameter items, url
is the requested service address.
Execute the request without using parameters: curl http://www.baidu.com. In this case, the command execution will directly request the specified service address and output the source code of the request result to the Linux command line.
Related parameters:
The curl command implements rich functions through different parameter items. Common parameter items are:
-i, i.e. --head, indicates that the request result displays the response header information
-o, i.e. --output, followed by the file path, indicates that the request result is written to To the specified file
-s, that is, --silent, silent mode, which means that no additional information will appear in the request result at this time
- w, that is, --write-out [format], the parameter is followed by the format string, indicating the specified output content after the command is executed.
Usually you can use the command when testing whether the interface is normal:
curl -o /dev/null -s -w %{http_code} http://www.baidu.com
At this time, the return source code of the service is written into the empty address null, and the silent mode is turned on, and the http status code is output after the request. If the request is successful, 200 is returned.
For more available parameters of curl, you can use curl -h/--help to query.
Use curl to upload and download files
1. File upload
provided in curl For the command parameters of uploading files, you can initiate a request through -F followed by file information.
curl -F 'file=@test.png;type=image/png' http://www.baidu.com/upload
Among them, it should be noted that:
When using the -F parameter, curl considers it to be an uploaded file and will specify it by default. -H Content-Type: multipart/form-datas, indicating the use of file upload format
When the -F parameter specifies file information, you can specify multiple files and file types. Multiple parameters are separated by;
2. File download
There are many ways to download files in the curl command, namely:
For the result data obtained by the request, you can use the Linux method The redirect function saves to a file...>> index.html
Use the -o/-O parameter to request the file and save it,curl -o a.jpg http://www.baidu.com/b.jpg
When requesting to download a file, you can specify the -# parameter at the same time, which will display the progress of the file download. Use - sClose
If the file download process stops, you can add the -C parameter to execute the breakpoint resume download of the file
Use curl to simulate GET/POST requests
The curl command supports POST requests, cookies, authentication and other operations, so it can also be well used to simulate service interface requests during the development process. .
1. Simulate GET request
The GET request is actually the simplest service request. It carries parameter information through URL splicing, so it is easiest to use curl directly. Just request it.
Simulating GET requests to obtain data during the development process:
curl http://localhost:8080/getUserInfo?id=1。
2. Simulating POST requests
POST requests are a way to submit data. When requesting the service address, parameter information will be carried for submission. The curl command also provides a method for carrying parameter information.
-X, the parameter is followed by the request method, you can perform a POST request
-H, the parameter is followed by a string, you can specify the request header information, For example, "Content-Type:application/json" means transmitting data in json format
-d, and the parameter is followed by a string. You can specify the request parameter content in the form of a string, using -d Please note when using parameters
This command can be used multiple times to specify multiple parameter information
也可以使用 -d 'key1=val1&key2=val2' 来一次指定多个参数
如果指定了参数为json类型,则可以使用 -d '{"id":1,"name":"shone"}' 来传递一个json对象
使用 -d 参数后,请求类型会默认为POST,此时可以省略 -X 参数的指定
-d参数还可以指定文件作为参数,使用@后跟文件名的方式,-d "@test.txt"
对于一个完整的POST请求可以是:
curl -H "Content-Type:application/json" -d '{"id":1,"name":"shone"}' http://localhost:8080/queryInfo
3、使用 Cookies
浏览器在进行服务请求时,通常会保存请求信息到Cookies中,可以使用 -D 参数来指定保存请求的Cookies
curl -o page1.html -D cookie.txt http://www.baidu.com
curl 命令还可以模拟浏览器的请求,使用 -A 指定浏览器信息来模仿浏览器发起请求并保存Cookies
curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -o page.html -D cookie.txt http://mydomain.net
保存的Cookie信息,可以在请求时使用-b参数指定携带Cookie
curl -b cookie.txt http://www.baidu.com
相关推荐:《Linux视频教程》
The above is the detailed content of Is curl installed by default on linux?. For more information, please follow other related articles on the PHP Chinese website!