简体   繁体   中英

Ruby - open-uri doesn't download a file itself, but just the HTML code of the website

I am trying to use this snippet:

  open("data.csv", "wb") do |file|
    file << open("https://website.com/data.php", http_basic_authentication: ["username", "password"]).read
  end

But instead of the desired CSV file, I get just downloaded the HTML code of the website. What's the problem? When I access the URL and I am not logged in, then it's displayed the form for login (not the HTTP authentication window). How to solve this situation?

Thanks

我认为您应该尝试net / http http://dzone.com/snippets/how-download-files-ruby

It's probably because your php script return a response with a mime-type different of text/plain or better : text/csv

please see this related previous response

How to use the CSV MIME-type?

in the PHP Script :

header('Content-Type: text/csv');
header('Content-disposition: attachment;filename=data.csv');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM