4

Trying to download a file through CMD line but I have spaces in the filename preventing the download.

curl --request GET -v --user user$site:password https://www.whatever.com/uploads/Filename With Spaces.csv > /users/kanye_west/desktop/FilenameWithSpaces.csv

Tried escaping the spaces with backslash \ already to no avail

5
  • Spaces in url isn't specified as a space. Use the ASCII hex code to specify this. Your URL should be "https://www.whatever.com/uploads/Filename%20With%20Spaces.csv", where %20 means space. Commented Jan 22, 2016 at 3:27
  • a valid URI (RFC3986) cannot contain spaces, but if you enclosed the URL with double quotes it would probably still work.... Commented Jan 22, 2016 at 7:54
  • @DanielStenberg - even with double quotes, space is not allowed in urls. URLs must be encoded to accommodate space. Commented Jan 22, 2016 at 22:38
  • true, but curl will accept a URL with spaces when double quotes are used Commented Jan 22, 2016 at 22:39
  • curl will pass the space as is and web service will not know how to handle the space. Hence the need to encode the spaces. Commented Jan 22, 2016 at 22:43

5 Answers 5

5

Use %%20 instead of space if it is in the windows batch file. As %20 in batch file means 20th parameter. So for encoding % is used %%.

Sign up to request clarification or add additional context in comments.

Comments

4

Replace each space by %20

curl --request GET -v --user user$site:password https://www.whatever.com/uploads/Filename%20With%20Spaces.csv > /users/kanye_west/desktop/FilenameWithSpaces.csv

Comments

3

Put the URL in quotes.

curl --request GET -v --user user$site:password "https://www.whatever.com/uploads/Filename With Spaces.csv" > /users/kanye_west/desktop/FilenameWithSpaces.csv

Backslashes would work on Unix, but they aren't used as an escape charater on Windows because they're the original form of directory separator.

2 Comments

I get a http 400 error when trying.. The command works fine for a file without spaces though
If the URL were wrong you would get a 404 error. So the problem is probably somewhere else.
1

Here's a cheap trick to encode urls. It encodes more than just spaces.

urlencode() {
    # urlencode <string>

    local length="${#1}"
    for (( i = 0; i < length; i++ )); do
        local c="${1:i:1}"
        case $c in
            [a-zA-Z0-9.~_-:/]) printf "$c" ;;
            *) printf '%%%x' \'"$c" ;;
        esac
    done
}

You can then use this function to convert your urls before passing to curl.

curl --request GET -v --user user$site:password $(urlencode "https://www.whatever.com/uploads/Filename With Spaces.csv") > /users/kanye_west/desktop/FilenameWithSpaces.csv

Comments

0

The quoted url wasn't working in my case and I really did not want to have to manually embed the %20. So I turned to powershell to urlencode:

powershell "\"url=\"\"\" + [uri]::EscapeUriString(\"https://www.whatever.com/uploads/Filename With Spaces.csv\") + \"\"\"\"" > yaddah.txt && curl --request GET -v --user user$site:password -K yaddah.txt

Powershell encodes the spaces and then saves the resulting url parameter to a file, which is in turn used as an input to curl.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.