在Unix 和Unix-like 系统中,以下提交请求,将响应头写入控制台并将响应正文保存到output.pdf:
curl -F file=@"c:\Word.docx" http://do.convertapi.com/Word2Pdf \
--output output.pdf --dump-header /dev/fd/1 --silent | tail -n +2
说明
--output output.pdf 选项将结果输出到output.pdf。
-o, --output <file>
Write output to <file> instead of stdout.
--dump-header /dev/fd/1 选项将 HTTP 状态行(例如 HTTP/1.1 200 OK)和响应标头写入标准输出(即控制台)。 --dump-header 选项将标头转储到给定文件。 /dev/fd/1 是标准输出的 file descriptor,因此写入它的任何内容都会输出到控制台(或标准输出通过管道或重定向到任何地方)。
-D, --dump-header <filename>
(HTTP FTP) Write the received protocol headers to the specified file.
--silent 选项使得只输出标题,而不是进度条。
-s, --silent
Silent or quiet mode. Don't show progress meter or error messages. Makes Curl mute. It will still output the data you ask for,
potentially even to the terminal/stdout unless you redirect it.
curl 的结果是piped 到tail 命令tail -n +2,它丢弃了第一行内容,即HTTP 状态行(例如HTTP/1.1 200 OK)。如果您希望或不介意同时拥有状态行和标题,则可以省略此 tail 管道部分。
-n, --lines=[+]NUM
output the last NUM lines, instead of the last 10; or use
-n +NUM to output starting with line NUM
示例
$ curl http://www.example.com --output test.html \
--dump-header /dev/fd/1 --silent | tail -n +2
Age: 466166
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Wed, 27 Oct 2021 16:49:42 GMT
Etag: "3147526947+gzip+ident"
Expires: Wed, 03 Nov 2021 16:49:42 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECS (chb/0286)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256