Ruby Simple Http Post without NET::HTTP Complexity

I just got brain damage again while trying to use NET::HTTP, its so ridiculous complex, why is there no simple alternative ?
(you can use open-uri for get, but post/delete/update ?)

  def post(url,data)
    require 'activesupport'
    require 'net/http'
    url = URI.parse(url)
    http = Net::HTTP.new(url.host, url.port)
    if url.scheme == 'https'
      require 'net/https'
      http.use_ssl = true
    end
    resp, data = http.post(url.path, data.to_query)
    raise "POST FAILED:"+resp.inspect unless resp.is_a? Net::HTTPOK or resp.is_a? Net::HTTPFound
    return data
  end

I am willing to make a SimpleHTTP gem if no one comes up with a satisfactory solution 😦

SimpleHTTP::get(url,data)
SimpleHTTP::post(url,data)
SimpleHTTP::delete(url,data)
SimpleHTTP::update(url,data)

response = page content, throw exception when response code not 200

UPDATE:
rest-client seems to be a good solution, although it does not support sending files.

2 thoughts on “Ruby Simple Http Post without NET::HTTP Complexity

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s