cURL:如何在上传时显示进度信息?

Det*_*ant 40 curl

我使用以下语法上传文件:

curl --form upload=@localfilename --form press=OK [URL]
Run Code Online (Sandbox Code Playgroud)

如何显示进度?谢谢.

cho*_*own 63

这是我在我的一个构建脚本中使用的:

curl "${UPLOAD_URL}" \
    --progress-bar \
    --verbose \
    -F build="${BUILD}" \
    -F version="${VERSION}" \
    -F ipa="@${IPA};type=application/octet-stream" \
    -F assets="@-;type=text/xml" \
    -F replace="${REPLACE}" \
    -A "${CURL_FAKE_USER_AGENT}" \
    <<< "${ASSETS}" \
    | tee -a "${LOG_FILE}" ; test ${PIPESTATUS[0]} -eq 0
Run Code Online (Sandbox Code Playgroud)

-F-A选项可能不会有兴趣的你,但有用的部分是:

curl "${UPLOAD_URL}" --progress-bar
Run Code Online (Sandbox Code Playgroud)

它告诉curl在上传过程中显示进度条(而不是默认的"进度表"),并且:

 | tee -a "${LOG_FILE}" ; test ${PIPESTATUS[0]} -eq 0
Run Code Online (Sandbox Code Playgroud)

它将命令的输出附加到日志文件,并将其回显到stdout.该test ${PIPESTATUS[0]} -eq 0部分使得该行的退出状态(在bash脚本中)与curl命令返回的退出代码相同,而不是命令的退出状态tee(因为tee实际上是该行中执行的最后一个命令所必需的),不是curl).


来自man curl:

PROGRESS METER
       curl normally displays a progress meter during operations, indicating the amount of transferred data, transfer speeds and estimated time left, etc.

       curl  displays  this  data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the terminal, it disables the progress meter as otherwise it would mess up the
       output mixing progress meter and response data.

       If you want a progress meter for HTTP POST or PUT requests, you need to redirect the response output to a file, using shell redirect (>), -o [file] or similar.

       It is not the same case for FTP upload as that operation does not spit out any response data to the terminal.

       If you prefer a progress "bar" instead of the regular meter, -# is your friend.

OPTIONS
       -#, --progress-bar
              Make curl display progress as a simple progress bar instead of the standard, more informational, meter.
Run Code Online (Sandbox Code Playgroud)

  • “如果您想要 HTTP POST 或 PUT 请求的进度表,则需要使用 shell 重定向 (&gt;)、-o [文件] 或类似方法将响应输出重定向到文件。” 这应该突出显示,每次都让我感动。 (5认同)
  • 或者:`&gt; / dev / null` (2认同)

rug*_*ugk 34

这里的所有其他答案都有一个问题,即它们要求您将curl的原始输出写入(日志)文件.然而,在所有情况下都不需要这样做.

问题是当预期服务器响应时,curl会隐藏进度条/米,然后将其写入sdout.因此,基本上您可以将输出重定向到文件以再次显示该栏.但是,我们并不希望出现这种情况,所以/dev/nulltee可以帮助我们在这里:

curl --progress-bar -T "${SOME_LARGE_FILE}" "${UPLOAD_URL}" | tee /dev/null
Run Code Online (Sandbox Code Playgroud)

Curl的输出传递给tee控制台(我们希望看到进度条和服务器响应)并将其写入文件(我们不需要它,但是因为我们使用/dev/null它并不重要).

请注意,curl开发人员当然没有隐藏进度条以获得乐趣.在这种情况下,您可能并不总是看到服务器结果,或者它可能只显示几秒钟,但如果您不关心这一点,那么解决方案就是一个不错的解决方案.

  • 为什么 `curl --progress-bar ... | 三通/dev/null`?就我而言,`curl --progress-bar ... | cat` 似乎也能正常工作。 (6认同)
  • 我认为他们只是为了好玩而隐藏了它,因为您明确要求使用`--progress-bar`来显示进度条。 (4认同)

Mar*_*lme 25

我在接受答案的命令重定向时遇到问题,发现该-o选项会将响应输出放在一个允许进度条显示的文件中.

curl --progress-bar \ 
     -o upload.txt \
     -H ${SOME_AUTH_HEADER} \
     -T ${SOME_LARGE_FILE} \
     "${UPLOAD_URL}"
Run Code Online (Sandbox Code Playgroud)

只是获得所需结果的另一种选择.


注意:从手册页重点强调这一行对于理解仅在指定时进度条未显示的根本原因非常重要--progress-bar.

      If you want a progress meter for HTTP POST or PUT requests, 
      you need to redirect the response output to a file, 
      using shell redirect (>), -o [file] or similar.
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你!天哪,花了太长时间才弄清楚这一点。 (2认同)
  • 这应该是公认的答案。 (2认同)

小智 5

要在 Windows 上显示进度条:curl --progress-bar --upload-file Myfile.zip "https://my.upload.site/Some/Upload/Path/Myfile.zip" | 类型