macOS CURL简单使用

文章介绍了Curl这款命令行工具在Linux服务器中的广泛用途,如网络健康检查、发送HTTP请求、处理各种协议、文件下载以及其在没有安装其他工具时的便利性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

最近做项目,需要服务器实现网络是否通畅,比如通过健康检查接口,但是只能linux服务器测试,很可能还需要测试h2,所以想到了curl,整理一版简单用法,以macOS为例,实际上macOS上的curl对比Linux略有区别。以8.7.1为例

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

发送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

发送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,-H可以多次使用,带多个header

              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

体验一下,实际在服务器上,可以用于生产验证:

curl -d '{"name":"tom","age":25}' -H "Content-Type: application/json" http://localhost:8080/demo

结果:

忽略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

实际示例demo:如果是https请求,则可以使用-k跳过ssl证书的认证,一般是配合-H -d使用

curl -k -H "xxx:xxx" -H "abc:cde" -d '{"":""}' url

ssl证书认证有2种关键处理:1、host校验;2、证书链的校验

host校验是最开始的步骤,验证域名(一般请求而言,域名即host),证书分为域名证书和IP证书,一般为域名证书,因为IP可能会变。

证书一般服务器内置了一些,比如macOS的系统证书和根证书

但是对于Linux服务器,很可能是私有证书,没经过认证,或者Linux服务器没连接互联网,证书没更新过期等,就需要跳过证书认证了。 

证书链认证的原理很简单,就是验签和数据核对,逐层验证,最上层为根证书,是CA颁发的

发送H2请求

--http2
              (HTTP) Tells curl to use HTTP version 2.

Providing --http2 multiple times has no extra effect.

              Example:
               curl --http2 https://example.com

etg:

当然新版本curl可以自动解析http 2.0版本协议,旧版本确不一定可以,笔者的老电脑就不行,但是新电脑预装8.7.1的curl,可以自动识别

 

下载文件

-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

etg:比如我下载一个maczip

当前目录就可以看到下载的文件了

wget

macOS没有wget,可以使用刚刚的curl替代

说实在,图形界面不是很需要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.

etg:略,这个在Linux服务器很熟悉了,下载软件或者依赖包经常用

总结

通过上面的分析,实际上curl一个命令就可以干很多命令的事情,功能非常强大。只是有时候我们形成固定习惯了,但是在有些命令不能安装,或者没机会安装的时候,curl可以用来执行各种命令,非常方便,而且基本上都操作系统内置了,尤其是linux服务器领域,基本上都可以使用这个程序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值