I know this is bad mvc, but i have a model class that needs to send a
request off to a web service. At the moment i’m building a request
string with url and params, and then doing a ‘system’ call to call curl
with the url. This feels pretty dirty though. Is there a nicer way to
just fire off an http request?
I know this is bad mvc, but i have a model class that needs to send a
request off to a web service. At the moment i’m building a request
string with url and params, and then doing a ‘system’ call to call
curl
with the url. This feels pretty dirty though. Is there a nicer way
to
just fire off an http request?
http = Net::HTTP.new(“#{domain}”)
headers, body = http.get(“#{path}?#{params}”)
Yuck. would be a lot easier to take a hash of params (ie {‘l_cid’ =>
‘1106’, ‘l_key’ => ‘cg3608898b74a4c688787ab479b8bb9f’, …} and then
call to_query on it.
Net::HTTP will also do that for you, with something like
response = Net::HTTP.start(host, port) do |http|
get = Net::HTTP::Get.new path
get.form_data = some_hash
get.content_type = “application/x-www-form-urlencoded”
http.request get
end
The params string had spaces and & signs in it, so i just replaced them
with safe characters temporarily while i played with Net::HTTP (with
escaping them properly down for a later task).
I tried this:
http = Net::HTTP.new(“#{domain}”)
headers, body = http.get(“#{path}?#{params}”)
and got a 401 (Authorisation required) error back. I think maybe my
params weren’t going through properly.
I totally agree, i actually build up all the params in a hash anyway.
Here it is in your recommended format. One of the params has the
complication of actually being a full url with params itself. Curl
seems to deal happily with this, i’m guessing it escapes everything
nicely. But trying this, i get the same 401 error back
(#<Net::HTTPUnauthorized 401 Authorization Required readbody=true>).
host = “www.l-mail.biz”
port = 80
path = “/scripts/lia/lia.php”
response = Net::HTTP.start(host, port) do |http|
get = Net::HTTP::Get.new path
get.form_data = param_hash
get.content_type = “application/x-www-form-urlencoded”
http.request get
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.