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?
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.
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