0

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

user984621
  • 46,344
  • 73
  • 224
  • 412
  • Can you share more data about the request to the website? I think that the request is not being formed correctly. – JunaidKirkire Oct 23 '13 at 11:52
  • When I access **https://website.com/data.php** and I am not logged in, I am redirected on the page that is on this url - **https://website.com/login.php?go=data.php**. I was talking with admin and he told me that this can be solved with CURL (in PHP). So I am looking for the way to do it in Ruby. – user984621 Oct 23 '13 at 11:57
  • your code in ruby is OK IF and only IF Content mime type is text/plain – Romain Oct 23 '13 at 12:00
  • it's just a pbm of mime type in header , if you canno't change it, you have to parse output – Romain Oct 23 '13 at 13:41

2 Answers2

0

I think you should try out net/http http://dzone.com/snippets/how-download-files-ruby

JunaidKirkire
  • 878
  • 1
  • 7
  • 17
0

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');
Community
  • 1
  • 1
Romain
  • 451
  • 4
  • 13
  • In the **data.php** script is generated CSV files. When I download this file manually, it's the classic CSV file. It's not my website nor my script, I am fetching these data form a supplier. – user984621 Oct 23 '13 at 12:09