Why won’t curl download this link when a browser will

curl

I am running Mac OS 10.11.6 El Capitan. There is a link I would like to download programmatically:

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-osx10.11-x86_64.dmg

If I paste this URL into any browser (e.g. Safari) the download works perfectly.

However, if I try to download the same URL from the command line using curl, it doesn't work—the result is an empty file:

$ ls -lA
$ curl -O https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-osx10.11-x86_64.dmg
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
$ ls -lA
total 0
-rw-r--r--  1 myname  staff  0 Nov  7 14:07 mysql-5.7.16-osx10.11-x86_64.dmg
$ 

Of course I can get the file through the browser, but I would like to understand why the curl command above doesn't work.

Why can't curl download this file correctly, when it is evidently present on the website and can be correctly accessed and downloaded through a graphical web browser?

Best Answer

There is a redirect on the webserver-side to the following URL: http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.16-osx10.11-x86_64.dmg. Because it's a CDN, the exact behaviour (whether you get redirected or not) might depend on your location.

curl does not follow redirects by default. To tell it to do so, add the -L argument:

curl -L -O https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-osx10.11-x86_64.dmg
Related Question