net/http broken?

Discussion in 'Ruby' started by tsuraan, Nov 11, 2005.

  1. tsuraan

    tsuraan Guest

    The documentation for net/http on ruby-doc.org gives the following
    snippet for getting data from a server using HTTP POST:

    require 'net/http'
    require 'uri'

    res =3D Net::HTTP.post_form(URI.parse('http://www.example.com/search.cg=
    i'),
    {'q'=3D>'ruby', 'max'=3D>'50'})
    puts res.body

    The problem with this is that in ruby 1.8.2 on both my OSX box and my
    FreeBSD computer, Net::HTTP does not have a post_form defined. So,
    that example doesn't work. Another way of doing the same idea, given
    by ruby-doc.org, is:

    url =3D URI.parse('http://www.example.com/todo.cgi')
    req =3D Net::HTTP::post.new(url.path)
    req.set_form_data({'from'=3D>'2005-01-01', 'to'=3D>'2005-03-31'}, ';')
    res =3D Net::HTTP.new(url.host, url.port).start { http.request(req) }
    case res
    when Net::HTTPSuccess, Net::HTTPRedirection
    # OK
    else
    res.error!
    end

    This has the problem that req has no set_form_data method. So, does
    anyone know what's going wrong? Is it possible to use the ruby
    standard libraries to post data to a server?
     
    tsuraan, Nov 11, 2005
    #1
    1. Advertising

  2. tsuraan wrote:
    > The documentation for net/http on ruby-doc.org gives the following
    > snippet for getting data from a server using HTTP POST:
    >
    > require 'net/http'
    > require 'uri'
    >
    > res =
    >
    >
    > Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
    > {'q'=>'ruby', 'max'=>'50'}) puts res.body
    >
    > The problem with this is that in ruby 1.8.2 on both my OSX box and my
    > FreeBSD computer, Net::HTTP does not have a post_form defined. So,
    > that example doesn't work. Another way of doing the same idea, given
    > by ruby-doc.org, is:
    >
    > url = URI.parse('http://www.example.com/todo.cgi')
    > req = Net::HTTP::post.new(url.path)
    > req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
    > res = Net::HTTP.new(url.host, url.port).start { http.request(req)
    > } case res
    > when Net::HTTPSuccess, Net::HTTPRedirection
    > # OK
    > else
    > res.error!
    > end
    >
    > This has the problem that req has no set_form_data method. So, does
    > anyone know what's going wrong? Is it possible to use the ruby
    > standard libraries to post data to a server?
     
    Robert Klemme, Nov 11, 2005
    #2
    1. Advertising

  3. tsuraan wrote:
    > The documentation for net/http on ruby-doc.org gives the following
    > snippet for getting data from a server using HTTP POST:
    >
    > require 'net/http'
    > require 'uri'
    >
    > res =
    >
    >
    > Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
    > {'q'=>'ruby', 'max'=>'50'}) puts res.body
    >
    > The problem with this is that in ruby 1.8.2 on both my OSX box and my
    > FreeBSD computer, Net::HTTP does not have a post_form defined.


    Hm, 1.8.3 has it:

    17:02:52 [~]: ruby -r net/http -e 'p RUBY_VERSION; p
    Net::HTTP.public_methods.grep /post/i'
    -e:1: warning: parenthesize argument(s) for future version
    "1.8.3"
    ["post_form"]

    This is on cygwin. I wouldn't assume that post_form was removed in 1.8.2
    and added in 1.8.3. Are you sure the method is not present? (You can
    test with the same code snippet.)

    Kind regards

    robert
     
    Robert Klemme, Nov 11, 2005
    #3
  4. tsuraan

    James Britt Guest

    tsuraan wrote:
    > The documentation for net/http on ruby-doc.org gives the following
    > snippet for getting data from a server using HTTP POST:
    >
    > require 'net/http'
    > require 'uri'
    >
    > res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'),
    > {'q'=>'ruby', 'max'=>'50'})
    > puts res.body
    >
    > The problem with this is that in ruby 1.8.2 on both my OSX box and my
    > FreeBSD computer, Net::HTTP does not have a post_form defined. So,
    > that example doesn't work. Another way of doing the same idea, given
    > by ruby-doc.org, is:
    >
    > url = URI.parse('http://www.example.com/todo.cgi')
    > req = Net::HTTP::post.new(url.path)
    > req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';')
    > res = Net::HTTP.new(url.host, url.port).start { http.request(req) }
    > case res
    > when Net::HTTPSuccess, Net::HTTPRedirection
    > # OK
    > else
    > res.error!
    > end


    Seems the docs are wrong. I had the same problem not too long ago.

    Here's an example of what works for me:

    require 'net/http'

    # post some data to uri:port/page

    uri = 'www.example.com'
    port = 80
    page = '/RPC/foo'
    data = '<foo><bar>Doodle!</bar></foo>'

    Net::HTTP.start( uri, port) { |http|
    results = http.post( page, data )
    }


    James

    --

    http://www.ruby-doc.org - Ruby Help & Documentation
    http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
    http://www.rubystuff.com - The Ruby Store for Ruby Stuff
    http://www.jamesbritt.com - Playing with Better Toys
    http://www.30secondrule.com - Building Better Tools
     
    James Britt, Nov 11, 2005
    #4
  5. tsuraan

    tsuraan Guest

    > Hm, 1.8.3 has it:
    >
    > 17:02:52 [~]: ruby -r net/http -e 'p RUBY_VERSION; p
    > Net::HTTP.public_methods.grep /post/i'
    > -e:1: warning: parenthesize argument(s) for future version
    > "1.8.3"
    > ["post_form"]


    Hm, I get just "1.8.2" when I run that. Same thing on my apple and my
    freebsd box. Strange...
     
    tsuraan, Nov 11, 2005
    #5
  6. tsuraan

    tsuraan Guest

    > Seems the docs are wrong. I had the same problem not too long ago.
    >
    > Here's an example of what works for me:
    >
    > require 'net/http'
    >
    > # post some data to uri:port/page
    >
    > uri =3D 'www.example.com'
    > port =3D 80
    > page =3D '/RPC/foo'
    > data =3D '<foo><bar>Doodle!</bar></foo>'
    >
    > Net::HTTP.start( uri, port) { |http|
    > results =3D http.post( page, data )
    > }


    I'll give that a shot. Thanks!
     
    tsuraan, Nov 11, 2005
    #6
  7. tsuraan

    James Britt Guest

    Robert Klemme wrote:
    ...

    >
    > Hm, 1.8.3 has it:
    >
    > 17:02:52 [~]: ruby -r net/http -e 'p RUBY_VERSION; p
    > Net::HTTP.public_methods.grep /post/i'
    > -e:1: warning: parenthesize argument(s) for future version
    > "1.8.3"
    > ["post_form"]
    >
    > This is on cygwin. I wouldn't assume that post_form was removed in 1.8.2
    > and added in 1.8.3. Are you sure the method is not present? (You can
    > test with the same code snippet.)


    Ah. That explains my problem using the Windows installer version.
    There has been no 1.8.3 release, for assorted reasons, and I forgot that
    I'm therefore not running the latest Ruby.

    So my suggestion is a 1.8.2 bit of code.

    I believe Curt will release a 1.8.4 Windows installer version when Matz
    releases that version of Ruby around Christmas.

    James

    --

    http://www.ruby-doc.org - Ruby Help & Documentation
    http://www.artima.com/rubycs/ - Ruby Code & Style: Writers wanted
    http://www.rubystuff.com - The Ruby Store for Ruby Stuff
    http://www.jamesbritt.com - Playing with Better Toys
    http://www.30secondrule.com - Building Better Tools
     
    James Britt, Nov 11, 2005
    #7
  8. tsuraan

    daz Guest

    daz, Nov 12, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gert
    Replies:
    2
    Views:
    327
  2. Steven D'Aprano

    Why are "broken iterators" broken?

    Steven D'Aprano, Sep 21, 2008, in forum: Python
    Replies:
    8
    Views:
    684
  3. Cameron Simpson

    Re: Why are "broken iterators" broken?

    Cameron Simpson, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    608
    Cameron Simpson
    Sep 22, 2008
  4. Fredrik Lundh

    Re: Why are "broken iterators" broken?

    Fredrik Lundh, Sep 22, 2008, in forum: Python
    Replies:
    0
    Views:
    623
    Fredrik Lundh
    Sep 22, 2008
  5. nicholas.henry
    Replies:
    0
    Views:
    234
    nicholas.henry
    May 21, 2006
Loading...

Share This Page