【问题标题】:cURL to show response headers after submiting a filecURL 在提交文件后显示响应头
【发布时间】:2014-08-25 20:30:52
【问题描述】:

我将文件发布到 url 并希望读取 curl 控制台中的所有标题输出。我应该添加什么参数?

curl -F file=@"c:\Word.docx" http://do.convertapi.com/Word2Pdf > output.pdf

【问题讨论】:

  • 寻找-I?或者 -D 如果你想将标题转储到文件中
  • 如果我在 -F 之前添加 -I 会出错:警告:您只能选择一个 HTTP 请求! curl:选项 -F:在这里使用不当 curl:尝试“curl --help”或“curl --manual”以获取更多信息
  • I 发送 HEAD 请求,F 发送 POST,因此发生冲突。您需要i,因为它只是将标头添加到命令行输出并且不会影响请求。 (下面回答)
  • 您是否希望将 HTTP 响应正文写入 output.pdf,但在控制台中显示标头?

标签: curl


【解决方案1】:

使用-i

来自 cURL 手册

 -i, --include       Include protocol headers in the output (H/F)

另请注意:

-I, --head          Show document info only

第一个将显示标题,然后是正文。第二个将发送 HEAD 请求,因此在您发布数据时无法在您的示例中使用。

编辑

使用-i 的标头输出回显到标准输出,与请求正文相同,因此将响应定向到 PDF 文件将创建无效的 PDF。

所以我建议你改用-v,这会更吵,但在将标准输出定向到文件时会在命令行上显示标题,因为详细输出会发送到标准错误。

【讨论】:

  • 如果你有机会尝试执行 curl -i -F file=@"c:\Word.docx" do.convertapi.com/Word2Pdf > output.pdf 并且你不会在 curl 控制台中得到任何响应头。
  • 您将响应标头发送到您的 PDF 中,这也会使其成为无效的 PDF
  • 如何在 curl 控制台中获取和保存响应文件并获取输出标头?
  • 我明白了。刚刚更新了我的答案。 -v 可能更适合您的情况
【解决方案2】:

UnixUnix-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 的结果是pipedtail 命令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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    相关资源
    最近更新 更多