2011-01-12 11 views
0

Ich versuche auf Dreamhost API zugreifen, um eine E-Mail zu senden.Erstellen komplexer URL

Die API erwartet eine URL-Domains und die tatsächliche emailcontent

domain = 'https://api.dreamhost.com/' 
key = "dq86ds5qd4sq" 
command = "announcement_list-post_announcement" 
mailing = "mailing" 
domain = "domain.nl" 
listname = "mailing Adventure"<[email protected]>" 
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 
# } 
} 
enthält

Wenn ich analysiere die URL Ich erhalte einen Fehler

schlecht URI (nicht URI?)

Die URL enthält die URL selbst aus dem Listennamen und der Nachricht und ich nehme an, dies verursacht das Problem. Ich habe keine Ahnung, wie ich das angehen soll. CGI escpae wurde vorgeschlagen, aber das scheint weißen Raum in + zu konvertieren.

Jemand weiß, wie man das löst?

Dank

Antwort

2

Nach dieser Linie kommt

url = domain + "&key=#{key}&cmd=#{command}&listname=#{mailing}&domain=#{domain}&listname=#{listname}&message=#{message}" 

die URL heraus als:

"domain.nl&key=dq86ds5qd4sq&cmd=announcement_list-post_announcement&listname=mailing&domain=domain.nl&listname=mailing Adventure<[email protected]>&message=<html>html here</html>" 

, die keine richtige URL ist. Deshalb wird die Ausnahme generiert.

Eine schnelle Lösung ist wie folgt:

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 verwendet wird unformatiert Saiten zu entkommen.

Hoffe, das hilft.