前言
最近做项目,需要服务器实现网络是否通畅,比如通过健康检查接口,但是只能linux服务器测试,很可能还需要测试h2,所以想到了curl,整理一版简单用法。
curl
实际上curl是有官网的,只不过比较简陋curl - Documentation Overview
The libcurl sub section of the site is huge! 基于libcurl
以macOS为例:
实际上curl支持很多协议,包括可以发邮件,文件传输,包括熟悉的telnet,笔者还在用nc来telnet,实际上curl也可以
先看看
telnet
试试bing搜索页面, -t 表示可以进入telnet界面,通过-v Verbose,详细信息
curl -v telnet://202.89.233.101:443
只是有点啰嗦,通过协议方式连接telnet,不过好处是没有安装telnet,可以用curl一个命令即可
http
可以通过-d来发送Post请求
-d, --data <data>
(HTTP MQTT) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has
filled in an HTML form and presses the submit button.
如果有多行,可以\换行,类似springboot自动装配或者shell
-d, --data can be used several times in a command line
Examples:
curl -d "name=curl" https://example.com
curl -d "name=curl" -d "tool=cmdline" https://example.com
curl -d @filename https://example.com
可以通过-H发送header
同理可以发送多次,居然支持邮件协议,👍🏻,一般常用来传递content-type
-H, --header <header/@file>
(HTTP IMAP SMTP) Extra header to include in information sent. When used within an HTTP request, it is added to the regular
request headers.
-H, --header can be used several times in a command line
Examples:
curl -H "X-First-Name: Joe" https://example.com
curl -H "User-Agent: yes-please/2000" https://example.com
curl -H "Host:" https://example.com
curl -H @headers.txt https://example.co
可以通过-k忽略ssl验证
如果是https,那么可以忽略证书认证,这个在测试ssl时很常用
-k, --insecure
(TLS SFTP SCP) By default, every secure connection curl makes is verified to be secure before the transfer takes place. This
option makes curl skip the verification step and proceed without checking.
Providing -k, --insecure multiple times has no extra effect. Disable it again with --no-insecure.
Example:
curl --insecure https://example.com
可以通过--http2发送H2请求
--http2
(HTTP) Tells curl to use HTTP version 2.
Providing --http2 multiple times has no extra effect.
Example:
curl --http2 https://example.com
其他可以看文档,实际上很简单,可能不同系统参数略有差别,比如linux
可以支持下载文件
-o, --output <file>
Write output to <file> instead of stdout.
You may use this option as many times as the number of URLs you have. For example, if you specify two URLs on the same command
line, you can use it like this:
curl -o aa example.com -o bb example.net
-o, --output can be used several times in a command line
Examples:
curl -o file https://example.com
curl "http://{one,two}.example.com" -o "file_#1.txt"
curl "http://{site,host}.host[1-5].com" -o "#1_#2"
curl -o file https://example.com -o file2 https://example.net
wget
macOS没有wget
说实在,图形界面不是很需要wget,而且curl也可以支持wget,上面已经说明了,而且wget实际上是http1.0
笔者Ubuntu是最小化安装,还需要安装man的db包
执行unminimize,后
GNU Wget is a free utility for non-interactive download of files from the Web. It supports HTTP, HTTPS, and FTP protocols, as well as retrieval through HTTP proxies.
wget也支持http和https ftp协议,主要用于文件传输,貌似curl都支持,参数用法如下,一般就下下文件,当然也有很多玩法,主要请求http常规还是用curl
注意:下载文件必须知道文件大小 -- a chicken-and-egg problem.
Please be aware that Wget needs to know the size of the POST data in advance. Therefore the argument to "--post-file" must be a regular file; specifying a FIFO or
something like /dev/stdin won't work. It's not quite clear how to work around this limitation inherent in HTTP/1.0. Although HTTP/1.1 introduces chunked transfer that
doesn't require knowing the request length in advance, a client can't use chunked unless it knows it's talking to an HTTP/1.1 server. And it can't know that until it
receives a response, which in turn requires the request to have been completed -- a chicken-and-egg problem.
总结
通过上面的分析,实际上curl一个命令就可以干很多命令的事情,功能非常强大。只是有时候我们形成固定习惯了,但是在有些命令不能安装,或者没机会安装的时候,curl可以用来执行各种命令,非常方便,而且基本上都操作系统内置了,尤其是linux服务器领域,基本上都可以使用这个程序。