How do I save a file using the response header filename with cURL?

Curl

Curl Problem Overview


This question relates to the command line version of cURL.

I'm trying to download a file from a CGI script.

http://example.com/perl/dl.pl?ID=2

Using a browser the filename comes up as remotefilename.gz. cURL wants to save the file as dl.pl?ID=2.

How do I get cURL to save to a file with the filename from the response header?

Curl Solutions


Solution 1 - Curl

-J/--remote-header-name is the option you want.

You use -J in conjunction with -O, which makes curl use the file name part from the URL as its primary way to name the output file and then if there is a Content-disposition: header in the response, curl will use that name instead.

Solution 2 - Curl

curl http://example.com/dl.php?file=3123123 -O -J

if server uses redirection use these:

--location-trusted
--max-redirs 10

Solution 3 - Curl

curl -OJ gave me segmentation fault: 11 error when attempting to download multiple URLs at once.

Instead, I used wget. The latest version supports HTTP Content-Disposition headers, which often contain filename information.

wget --content-disposition http://example.com/download/url

Solution 4 - Curl

you can try the -o or -O option.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnView Question on Stackoverflow
Solution 1 - CurlDaniel StenbergView Answer on Stackoverflow
Solution 2 - CurlegirayView Answer on Stackoverflow
Solution 3 - CurljchookView Answer on Stackoverflow
Solution 4 - Curlghostdog74View Answer on Stackoverflow