Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL: How to display progress information while uploading?

Tags:

curl

I use the following syntax to upload files:

curl --form upload=@localfilename --form press=OK [URL]

How to display the progress? Thx.

like image 599
Determinant Avatar asked Apr 02 '12 08:04

Determinant


4 Answers

This is what I use in one of my build scripts:

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

The -F and -A options will probably not be of interest to you, but the helpful parts are:

curl "${UPLOAD_URL}" --progress-bar

which tells curl to show a progress bar (instead of the default 'progress meter') during the upload, and:

 | tee -a "${LOG_FILE}" ; test ${PIPESTATUS[0]} -eq 0

which appends the output of the command to a log file and also echo's it to stdout. The test ${PIPESTATUS[0]} -eq 0 part makes it so that the exit status of this line (which is in a bash script) is the same exit code that the curl command returned and not the exit status of the tee command (necessary because tee is actually the last command being executed in this line, not curl).


From 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.
like image 132
chown Avatar answered Oct 13 '22 20:10

chown


All other answer here have the problem that they require you to write the original output of curl into a (log) file. This may however not be wanted in all cases.

The issue is that curl hides the progress bar/meter, when a server response is expected, which is then written into stdout. So basically you can redirect the output to a file to show the bar again. However we do not want that, so /dev/null and tee can help us here:

curl --progress-bar -T "${SOME_LARGE_FILE}" "${UPLOAD_URL}" | cat

Curl's output is passed to tee which writes the output both to the console (which we want to see the progress bar and also the server response) and into a file (which is not needed by us, but as we use /dev/null this does not matter).

Note that the curl devs of course did not hide the progress bar for fun. In this case here you may not always see the server result or it may only be shown a few seconds (as the progress bar is shown again afterwards), but if you do not care about this, the solution is a nice one.

like image 40
rugk Avatar answered Oct 13 '22 19:10

rugk


I had trouble with the accepted answer's command redirection and found the -o option will place the response output in a file which allows the progress bar to show up.

curl --progress-bar \ 
     -o upload.txt \
     -H ${SOME_AUTH_HEADER} \
     -T ${SOME_LARGE_FILE} \
     "${UPLOAD_URL}"

Just another option to get the desired result.


NOTE: Emphasis on this line from the man page is important to understand the root cause of why the progress bar is not showing when just specifying --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.
like image 33
Martin Woolstenhulme Avatar answered Oct 13 '22 21:10

Martin Woolstenhulme


To show progress bar on Windows: curl --progress-bar --upload-file Myfile.zip "https://my.upload.site/Some/Upload/Path/Myfile.zip" | type

like image 22
LukeInDC Avatar answered Oct 13 '22 20:10

LukeInDC