Search code examples
curl

cURL: How to display progress information while uploading?


I use the following syntax to upload files:

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

How to display the progress? Thx.


Solution

  • 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.