Reputation: 8689
How can I call some webservices in rails 3 using POST params to an external URL? I usually use something like this to post from the command line
curl -X POST -u "v10REVkw:XuCqIhyCw" \
-H "Content-Type: application/json" \
--data '{"apids": ["c80-d363237c-e513b"], "android": {"alert": "Android Test!"}}' \
https://url/to/push
Upvotes: 0
Views: 654
Reputation: 1761
Interesting enough that no-one hasn't so far suggested HTTParty, which is imho one of the simplest HTTP libs out there :)
http://httparty.rubyforge.org/
Upvotes: 0
Reputation: 115511
Here is a detailled way to achieve what you expect extracted from one of my gems.
Just adapt and trigger with remote
.
def remote
begin
Net::HTTP.get_response((uri)).body
rescue
#what you want here
end
end
def gateway
'http://api.opencalais.com/enlighten/rest/'
end
def uri
URI.parse(gateway + '?' + URI.escape(post_params.collect{ |k, v| "#{k}=#{v}" }.join('&')))
end
def post_params
{
'licenseID' => @api_key,
'content' => @context
}
end
EDIT:
Be sure you have this included:
require 'uri'
require 'net/http'
require 'open-uri'
EDIT2:
If you're interacting with RESTful webservice, consider using ActiveResource
Upvotes: 0
Reputation: 24783
I think I would use Rest client. Here is an example straight from their docs:
RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json
Upvotes: 2