Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending POST in ruby?

Tags:

post

ruby

any good library to send POST headers in ruby ?

like image 847
KJW Avatar asked Aug 22 '10 20:08

KJW


4 Answers

You can do something like this...

require 'net/http'

postData = Net::HTTP.post_form(URI.parse('http://thewebsite.net'), {'postKey'=>'postValue'})

puts postData.body
like image 107
krx Avatar answered Nov 20 '22 10:11

krx


The standard library Net::HTTP is pretty straightforward and handles POST.

From the docs:

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

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

For more detailed examples of how to use Net::HTTP, see August Lilleaas's Net::HTTP cheat sheet repository on Github.

like image 34
Telemachus Avatar answered Nov 20 '22 12:11

Telemachus


Net::HTTP as mentioned, the curl wrapper Curb, or HTTParty. Depending on what you are trying to do, they may be overkill.

like image 1
Chubas Avatar answered Nov 20 '22 10:11

Chubas


There are plenty of HTTP libraries in Ruby. There's the standard net/http and libcurl bindings. But there are also a lot of high-level libraries to consume web services such as ActiveResource and HTTParty. Depends on what you want to do. Maybe you can update your question with more information?

like image 1
AboutRuby Avatar answered Nov 20 '22 10:11

AboutRuby