Curl

来自姬鸿昌的知识库
跳到导航 跳到搜索

参考

https://www.ruanyifeng.com/blog/2019/09/curl-reference.html

https://4sysops.com/archives/how-to-use-curl-on-windows/#rtoc-3

https://www.booleanworld.com/curl-command-tutorial-examples/


安装

Linux

apk 安装
~ # apk add curl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/x86_64/APKINDEX.tar.gz
(1/4) Installing brotli-libs (1.0.9-r6)
(2/4) Installing nghttp2-libs (1.47.0-r0)
(3/4) Installing libcurl (7.83.1-r2)
(4/4) Installing curl (7.83.1-r2)
Executing busybox-1.35.0-r13.trigger
OK: 12 MiB in 21 packages
~ #

Windows

https://curl.se/windows/


使用示例

Curl 使用示例

Linux 版

curl --location --request GET 'https://www.douyin.com/user/abcd' \
--header 'accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
--header 'accept-encoding:  gzip, deflate, br' \
--header 'accept-language:  en' \
--header 'cache-control:  max-age=0' \
--header 'referer:  https://www.douyin.com/user/abcd' \
--header 'sec-ch-ua:  "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"' \
--header 'sec-ch-ua-mobile:  ?0' \
--header 'sec-ch-ua-platform:  "Windows"' \
--header 'sec-fetch-dest:  document' \
--header 'sec-fetch-mode:  navigate' \
--header 'sec-fetch-site:  same-origin' \
--header 'upgrade-insecure-requests:  1' \
--header 'user-agent:  Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
--output -


Windows 版

curl --location --request GET "https://www.douyin.com/user/abcd" ^
--header "accept:  text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" ^
--header "accept-encoding:  gzip, deflate, br" ^
--header "accept-language:  en" ^
--header "cache-control:  max-age=0" ^
--header "referer:  https://www.douyin.com/user/abcd" ^
--header "sec-ch-ua:  \"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"108\", \"Google Chrome\";v=\"108\"" ^
--header "sec-ch-ua-mobile:  ?0" ^
--header "sec-ch-ua-platform:  \"Windows\"" ^
--header "sec-fetch-dest:  document" ^
--header "sec-fetch-mode:  navigate" ^
--header "sec-fetch-site:  same-origin" ^
--header "upgrade-insecure-requests:  1" ^
--header "user-agent:  Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" ^
--output -


Windows 多行命令用 ^ 换行,Linux 里用 \ 换行;

--output 后面跟文件路径,指示将响应内容输出到文件;当后面跟 - 的时候表示无论如何(即使是非文本的二进制文件)输出到控制台中。

Linux 中 --header 后面的参数使用 '(单引号)包裹,Windows 中用 "(双引号)包裹,且对字符串中的双引号加以转义。



参数说明

-o 和 -O 不是一个意思

-O, --remote-name       Write output to a file named as the remote file

-o, --output <file>      Write to file instead of stdout

      --output-dir <dir>  Directory to save files in

-s 和 -sS 不是一个意思


-s/--silent

Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute.

不显示进度条


-S/--show-error

When used with -s it makes curl show an error message if it fails.

参数区分大小写


-L, --location           Follow redirects(3xx)