tags:

views:

145

answers:

2

How do you do a Remote HTTP Post (request) in ruby?

+5  A: 

With Net::HTTP. Correct but non-working example (because Google only supports GET — sorry, I couldn't think of any open POST forms off the top of my head):

require 'net/http'
require 'uri'

result = Net::HTTP.post_form(URI.parse("http://google.com/search"), {"q" => "Rick Astley"})

docs

Chuck
You are posting a form to a search which takes get parameters. Google will return you an error.
Mike Leffard
That's what I said. Google actually requires GET parameters, but I couldn't think of any good examples of commonly used POST forms off the top of my head. I think it pretty clearly illustrates how you do a POST, though.
Chuck
A: 

If the server conforms to REST you should take a look at either rest-client or ActiveResource.

Antti Tarvainen