Method: Gem::Net::HTTP.post_form

Defined in:
lib/rubygems/vendor/net-http/lib/net/http.rb

.post_form(url, params) ⇒ Object

Posts data to a host; returns a Gem::Net::HTTPResponse object.

Argument url must be a URI; argument data must be a hash:

_uri = uri.dup
_uri.path = '/posts'
data = {title: 'foo', body: 'bar', userId: 1}
res = Gem::Net::HTTP.post_form(_uri, data) # => #<Gem::Net::HTTPCreated 201 Created readbody=true>
puts res.body

Output:

{
  "title": "foo",
  "body": "bar",
  "userId": "1",
  "id": 101
}


890
891
892
893
894
895
896
897
898
# File 'lib/rubygems/vendor/net-http/lib/net/http.rb', line 890

def HTTP.post_form(url, params)
  req = Post.new(url)
  req.form_data = params
  req.basic_auth url.user, url.password if url.user
  start(url.hostname, url.port,
        :use_ssl => url.scheme == 'https' ) {|http|
    http.request(req)
  }
end