Search code examples
rubypostrubygemshttprequestopen-uri

POST with ruby: best practise and how to?


I want to make a simple post request using ruby. At first I tried it with the gem open-uri. But this answer to a so- question says that it doesn't work. So instead of this I want to use the rest-open-uri gem as recommended in the post. But how does it work? and how does it work using a proxy?

Thanks in advance!


Solution

  • http://ruby-doc.org/stdlib-2.0/libdoc/net/http/rdoc/Net/HTTP.html#method-i-post

    From the docs, a summary:

    response = http.post('/cgi-bin/search.rb', 'query=foo')
    

    Use case:

    # using block
    File.open('result.txt', 'w') {|f|
      http.post('/cgi-bin/search.rb', 'query=foo') do |str|
        f.write str
      end
    }