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

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?


Solution 1:

-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 http://example.com/dl.php?file=3123123 -O -J

if server uses redirection use these:

--location-trusted
--max-redirs 10

Solution 3:

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