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

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

I think you should try out 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');

Related

Ruby on Rails - get file from URL

I'm Using Amazon Ads API which giving me a URL as a response... Opening that URL in browser, giving me a file that I need... Problem is I Don't know how to get the file from the URL in ruby... can anyone help me???
Thanks
require 'open-uri'
contents = URI.open("https://hello.mdominiak.com").read
Documentation:
https://ruby-doc.org/stdlib-3.1.2/libdoc/open-uri/rdoc/OpenURI.html

how to upload a.zip file in Jmeter?

I am unable to upload a .zip file from Jmeter.
Upon trying to upload the file i am getting an error like below,
{
"args":[
"unsupportedMediaType"],
"message":"Request media type is not supported",
"messageId":"unsupportedMediaType",
"correlationId":"6539cd74-5f09-473c-40d2-36f98c0a472b",
"causes":[
],
"status":415
}
HTTP Method supported: POST.
Request header:
Content-Type: application/json; charset=UTF-8
Can anyone please help me in uploading the .zip file from the Jmeter, refer to the below image for my request,
Thanks in advance.
Are you sure about your content-type and what you are doing ?
for zip content type application/json is wrong.
fileToString will try to transform byes(zip) to text which cannot work
Try
checking Use multipart/form-data for Post
and Browser compatible headers .
remove FileToString call and use last tab Files upload instead of body data. And use there the content type application/zip or something suitable
If you are trying to upload a file and send a body, you may need to use nightly build.

how to read text file from django server using ajax?

I wanna know how to read text file from django server using ajax? or something else.
I searched about using XMLHttpRequest and It worked when I tryed to get django template html file. but I couldn't get other text file outside of templates directory. how can I send content of requested file in veiw.py ??
I think I cant use render_to_response()..
I need your help:(
Simething like this in your url:
url(r'^static/mymedia/' , 'myproject.views.get_file'),
Something like this in your views:
def get_file(request):
current_directory='/home/arpit/myproject/'
f=open(current_directory+request.path)
return HttpResponse(f.read(),mimetype='text/plain')

use html formatted uri in Ruby

I need to send mails via a webapi (Dreamhost). By accessing a url with encoded parameters you can define the email.
one of the parameters is the message in html format.
I use the open() method to acces the url but get a
URI::InvalidURIError
I can reformat the url by using URI.parse and URI.encode but this reformats the html in a unwanted state.
Is there a way I can embed html and open the url in Ruby?
Thanks
Use CGI.escape.
require 'cgi'
"http://example.com?message=#{CGI.escape('This is the message body!')}"

How to request for gzipped pages from web servers through ruby scripts?

I have a ruby script that goes and saves web pages from various sites, how do i make sure that it checks if the server can send gzipped files and saves them if available...
any help would be great!
One can send custom headers as hashes ...
custom_request = Net::HTTP::Get.new(url.path, {"Accept-Encoding" => "gzip"})
you can then check the response by defining a response object as :
response = Net::HTTP.new(url.host, url.port).start do |http|
http.request(custom_request)
end
p [response['Content-Encoding']
Thanks to those who responded...
You need to send the following header with your request:
Accept-Encoding: gzip,deflate
However, I am still reading how to code ruby and dont know how to do the header syntax in the net/http library (which I assume you are using to make the request)
Edit:
Actually, according to the ruby doc it appears the this header is part of the default header sent if you dont specify other 'accept-encoding' headers.
Then again, like I said in my original answer, I am still just reading the subject so I could be wrong.
For grabbing web pages and doing stuff with them, ScrubyIt is terrific.

Resources