Download file with url redirection

47,026

Use -L, --location to follow redirects:

$ curl -L http://httpbin.org/redirect/1
Share:
47,026
edem
Author by

edem

Full Stack Developer on Perl

Updated on December 08, 2020

Comments

  • edem
    edem over 3 years

    I can download a file by url but when I try it from bash I get a html page instead of a file.

    How to download file with url redirection (301 Moved Permanently) using curl, wget or something else?

    UPD

    Headers from the url request.

    curl -I http://www.somesite.com/data/file/file.rar

    HTTP/1.1 301 Moved Permanently
    Date: Sat, 07 Dec 2013 10:15:28 GMT
    Server: Apache/2.2.22 (Ubuntu)
    X-Powered-By: PHP/5.3.10-1ubuntu3
    Location: http://www.somesite.com/files/html/archive.html
    Vary: Accept-Encoding
    Content-Type: text/html
    X-Pad: avoid browser bug
    
    • slayedbylucifer
      slayedbylucifer over 10 years
      wget --content-disposition <URL> , Could you try this?
    • edem
      edem over 10 years
      Yes, I could. It doesn't work.
    • janos
      janos over 10 years
      What do you mean it doesn't work?
    • edem
      edem over 10 years
      I get the same result with this option.
    • Vijay Rajpurohit
      Vijay Rajpurohit about 4 years
      is this problem solved ?
  • edem
    edem over 10 years
    Get the content of the page from Location header. I've updated the question.
  • deltheil
    deltheil over 10 years
    Hmm... but if you try to download a file (a binary archive here) and the Location header redirects you to a HTML file then the archive is no more available... I assume this HTML page contains an error message, isn't it? If not, does it include information (i.e. link) related to the original file you're trying to download? In such a case you would have to parse the HTML to extract it.
  • edem
    edem over 10 years
    No, HTML page doesn't contain error info but it contain relative path to file: <a href="_blank>/data/file/file.rar</a> as I use with domain name to download the file but it doesn't work. I guess there is other domain name for its files but I don't know how to figure out it.
  • deltheil
    deltheil over 10 years
    So you need to first resolve this issue, then parse and reconstruct the right link to follow this redirection.
  • icasimpan
    icasimpan almost 7 years
    Works for me. Example, I'm downloading the jenkins war file which redirects, I could do curl -O -L http://mirrors.jenkins.io/war-stable/2.60.1/jenkins.war