15

curl 常用命令参考

 3 years ago
source link: https://www.wenyuanblog.com/blogs/curl-reference.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

curl 是常用的命令行工具,用来请求 Web 服务器。平时运维过程中使用频率较高。

curl 是一种命令行工具,作用是发出网络请求,然后得到和提取数据,显示在「标准输出」(stdout)上面。

它支持多种协议,下面整理平时运维过程中经常用到的几种场景。

一、查看网页源码

不带有任何参数时,curl 就是发出 GET 请求,返回网页源码。

curl https://www.baidu.com

上面命令向 https://www.baidu.com 发出 GET 请求,服务器返回的内容会在命令行输出。

二、文件下载

使用 -o 保存文件,类似于 wget 命令,比如下载 curl-7.73.0.zip 压缩包并保存为 curl.zip 文件。

curl -o jdk.tar.gz https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/linux/OpenJDK8U-jdk_x64_linux_hotspot_8u272b10.tar.gz

下载文件会显示下载状态,如数据量大小、传输速度、剩余时间等。可以使用 -s 参数禁用进度表。

curl -o jdk.tar.gz https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/linux/OpenJDK8U-jdk_x64_linux_hotspot_8u272b10.tar.gz -s

也可以使用 --process-bar 参数让进度表显示为进度条。

curl -o jdk.tar.gz https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/8/jdk/x64/linux/OpenJDK8U-jdk_x64_linux_hotspot_8u272b10.tar.gz --progress-bar

三、显示头信息

使用 -i 参数显示 Response Headers 信息,连同网页代码一起。使用 -I 参数可以只显示 Response Headers 信息。

curl -I https://www.baidu.com

四、请求方式(GET/POST/…)

使用 -X 参数指定 HTTP 请求的方法。

curl -X POST https://www.example.com

五、浏览器小技巧

通过 Chrome、Firefox 等浏览器可以直接拷贝请求为 cURL 语句。保存之后再请求测试非常方便。

QVZFBrF.png!mobile


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK