Matlab cURL instead of websave

Matlab websave might not work in cases where a plain “curl” or “wget” command works. A symptom of this issue is HTML is downloaded instead of the intended binary file. Websites such as Dropbox recognize the HTTP User Agent of cURL and Wget and mutate the web server response to be automation-friendly. Since Matlab is much less commonly used than Python, cURL, Wget, etc. this user agent-dependent behavior results. We recommend understanding why Matlab websave doesn’t work, or use the low-level Matlab HTTP Interface.


If you truly need to use cURL from Matlab, recognize this may require unique setup for each system, despite that cURL is included (preinstalled) in modern operating systems including Windows.

The extra quotes around “url” allow for arbitrary characters to be used in the URL that can confuse shells like zsh. The “-L” option to cURL allows redirects.

function curlsave(filename, url)

cmd = "curl -L -o " + filename + " '" + url + "'";

assert(system(cmd) == 0, "download failed: " + url)

end

Linux systems with multiple cURL versions installed may need to set an environment variable to prioritize. Set the filename as appropriate for computer (ensure the file exists).

export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libcurl.so.4

Even this doesn’t always work, so we recommend understanding why Matlab websave doesn’t work, or use the low-level Matlab HTTP Interface.