use html formatted uri in Ruby - 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!')}"

Related

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
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');

Ruby openuri test if uri is valid

I'm trying to test if a uri is valid (e.g. actually has content, not testing if it is well formed here) using ruby code, and I can open a uri using open(uri). But in my case, the uri is a link to a file to be downloaded and I don't want to have to download the whole file just to verify that there is content there.
Is there another solution for this?
Try this
require 'net/http'
u = URI.parse('http://www.example.com/')
status = Net::HTTP.start(u.host, u.port).head(u.request_uri).code
# status is HTTP status code
You'll need to use rescue to catch exception in case domain resolution fails.

What is difference between Response.Redirect("http://url") and Response.Write("REDIRECT=http://url")?

I'm working on ASP.NET MVC3 with C#.
What is difference between Response.Redirect("http://www.google.com"); and Response.Write("REDIRECT=http://www.google.com");?
The difference is that the first will replace the response with a redirection page and end the execution, while the second will just write the text to the response stream and continue with creating the rest of the page.
Response.Redirect() sets an HTTP 302 header along with the URL to be redirected to.
Response.Write("REDIRECT=http://www.google.com"); will write that string to the response body, as in that redirect text would be appended to the HTML of your web page.
This will create the correct full HTTP Header for you:
Response.Redirect("http://www.google.com");
You have the ability to set or change some paramters for the HTTP Header.
HttpResponse Class
e.g set HTTP Status Code 404 or 500 or in your case 302 for redirect.
e.g set the HTTP Mime-type for jpg
Will write into the Body in your response..like a string output
Response.Write("REDIRECT=http://www.google.com");
The methods in question are quite self explanatory :)
Response.Redirect("http://www.google.com");
The Redirect will redirect you to another page, in the case it will take you to Google's home page.
Response.Write("REDIRECT=http://www.google.com");
The Write method will write a string of text to the web page. In this case it will write the text "REDIRECT=http://www.google.com" to your web page.
Play around with these 2 methods in your web project and see what happens.

net/http post involving texts to a url using Ruby

I am writing a mobile App involving creating a Blogger client. I have the APIs that I need but the problem is how to write the ruby code for making a post using the texts beeing provided as well as making comments. The texts are supplied via form input but I don't know how to write the ruby code to post the text.
I will be very happy to recieve a response. Thanks all
From Net::HTTP documentation:
require 'net/http'
require 'uri'
#1: Simple POST
res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
{'q'=>'ruby', 'max'=>'50'})
puts res.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