'Curl - How can I get the filename when browser does, but Curl doesnt? [closed]

I'm trying to perform a simple download. I'm actually using a longer loop and getting the URLs from a filelist, but that's besides the point, since I encounter the error on normal singular Curl query either way, so I'll keep it as simple as possible.

When I try to download one of the files like that:

curl "https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/" -O

I get the following result:

curl: Remote file name has no length!
curl: (23) Failed writing received data to disk/application

However, if I copypaste this link into my Edge browser and just try to save the file, when downloading, a name pops up: 15608770555178647552_screenshots_20220310005322_1.jpg

How can I force Curl to get this name? What am I missing?

I'm using Windows 10 and the basic Command Prompt (cmd.exe).



Solution 1:[1]

unfortunately, as of writing, curl doesn't support UTF-8 filenames in Content-Disposition (see https://github.com/curl/curl/issues/1888 ), meaning you'll have to parse it out yourself.. if you have php-cli installed (see https://windows.php.net/ ), you could do

echo https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/ | php -r "if(0)var_dump(stream_get_contents(STDIN)); $ch=curl_init();$name='';curl_setopt_array($ch,array(CURLOPT_RETURNTRANSFER=>1,CURLOPT_HEADERFUNCTION=>function($ch,string $h)use(&$name):int{$pos=strpos($h,'filename*=UTF-8');if($pos!==false){$name=trim(substr($h,$pos+17,-3));}else{if(0)var_dump($h);} return strlen($h);},CURLOPT_URL=>trim(stream_get_contents(STDIN)) ));$bin=curl_exec($ch);file_put_contents($name,$bin);"

... but good luck doing that with just curl/cmd.exe..

Solution 2:[2]

Combine -O and -J

Try

curl -O -J "https://steamuserimages-a.akamaihd.net/ugc/1834659595661927431/A2B68B782314829F6BE9DA4034B144DFFD604887/"

If not told otherwise, curl writes the received data to stdout. It can be instructed to instead save that data into a local file, using the -o, --output or -O, --remote-name options. If curl is given multiple URLs to transfer on the command line, it similarly needs multiple options for where to save them.

Source

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2