creating complex url
I'm trying to access Dreamhost API to send a mail.
The API expects an url containing domains and the actual emailcontent
domain = 'https://api.dreamhost.com/' key = "dq86ds5qd4sq" command = "announcement_list-post_announcement" mailing = "mailing" domain = "domain.nl" listname = "mailing Adventure"<mailling@domain.nl>" message = '<html>html here</html>' url = domain + "&key=#{key}&cmd=#{command}&listname=#{mailing}&domain=#{domain}&listname=#{listname}&message=#{message}" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.start { # http.request_get(uri.path) {|res| # print res.body # } }
Wh开发者_运维知识库en I parse the url I get an error
bad URI(is not URI?)
The url contains url itself from the listname and message and I assume this causes the problem. I have no clue on how to approack this. CGI escpae has been suggested but that seems to convert white space into +.
Someone knows how to solve this?
Thanks
After this line
url = domain + "&key=#{key}&cmd=#{command}&listname=#{mailing}&domain=#{domain}&listname=#{listname}&message=#{message}"
the url is coming out as:
"domain.nl&key=dq86ds5qd4sq&cmd=announcement_list-post_announcement&listname=mailing&domain=domain.nl&listname=mailing Adventure<mailling@domain.nl>&message=<html>html here</html>"
which is not a proper url. That's why the exception is generated.
One quick fix is as follows:
url = "http://" + domain + "/?key=#{key}&cmd=#{command}&listname=#{mailing}&domain=#{domain}&listname=#{URI.escape(listname)}&message=#{URI.escape(message)}"
uri = URI.parse(url)
URI.escape is used to escape unformatted strings.
Hope this helps.
精彩评论