Post form example

Im looking for a very very basic POST form that will post data to an
external website with Net:http

I wonder if anyone has created one they could share?

Im looking for a very very basic POST form that will post data to an
external website with Net:http

I wonder if anyone has created one they could share?

From…
http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html

Posting Form Data

require 'net/http'
require 'uri'

#1: Simple POST
res = 

Net::HTTP.post_form(URI.parse(‘http://www.example.com/search.cgi’),
{‘q’=>‘ruby’, ‘max’=>‘50’})
puts res.body

#2: POST with basic authentication
res = 

Net::HTTP.post_form(URI.parse(‘http://jack:[email protected]/todo.cgi’),
{‘from’=>‘2005-01-01’,
‘to’=>‘2005-03-31’})
puts res.body

#3: Detailed control
url = URI.parse('http://www.example.com/todo.cgi')
req = Net::HTTP::Post.new(url.path)
req.basic_auth 'jack', 'pass'
req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
res = Net::HTTP.new(url.host, url.port).start {|http| 

http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

Or did you mean something else?

-philip

This is what I have used a few times. Error handling my own.

def self.post_data(url, post_data)
resp = “”
begin
url = URI.parse(url)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if url.port == 443
http.start do |http|
req = Net::HTTP::Post.new(url.path)
req.set_body_internal(post_data)
response = http.request(req)
resp = response.body
end
rescue Exception => e
@response_error = e
error = SystemError.new(:user_id => nil,
:account_id => nil,
:location => “GlobalFunctions.post_data”,
:error => “#{e}”,
:incidentals => {“url” => url,
“post_data” => post_data}
).save
end
resp
end

Crooksey wrote:

Im looking for a very very basic POST form that will post data to an
external website with Net:http

You don’t need Net::HTTP to do that. Just set the form’s action to an
external URL.

I wonder if anyone has created one they could share?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs