Net::HTTP - POST and GET in the same request

Discussion in 'Ruby' started by subsume@gmail.com, May 25, 2007.

  1. Guest

    I have a peculiar problem where I need to submit POST data to a script
    which has get variables (script.asp?variable=true)

    But Net:HTTP seems to have gone to great lengths to prevent this.
    Obviously, its messy and looked down upon but unfortunately I cannot
    control this.

    Running tests:

    body =
    Net::HTTP.post_form(URI.parse('http://www.server.net/post.php?x=y'),
    {'A'=>'1','B'=>'2','C' => '3'})

    puts body.body

    ---------

    Above, the POST variables are the only ones which hit the server.
    Apparently everything after the '?' is totally ignored. Please help!

    --
    Posted via http://www.ruby-forum.com/.
     
    , May 25, 2007
    #1
    1. Advertising

  2. On 25 May 2007, at 22:15, wrote:

    > I have a peculiar problem where I need to submit POST data to a script
    > which has get variables (script.asp?variable=true)
    >
    > But Net:HTTP seems to have gone to great lengths to prevent this.
    > Obviously, its messy and looked down upon but unfortunately I cannot
    > control this.
    >
    > Running tests:
    >
    > body =
    > Net::HTTP.post_form(URI.parse('http://www.server.net/post.php?x=y'),
    > {'A'=>'1','B'=>'2','C' => '3'})
    >
    > puts body.body
    >
    > ---------
    >
    > Above, the POST variables are the only ones which hit the server.
    > Apparently everything after the '?' is totally ignored. Please help!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    Try the following:

    http = Net::HTTP.new('http://www.server.net')
    response = http.post('/post.php', 'x=y', {'A'=>'1','B'=>'2','C' =>
    '3'})

    puts response.body

    Hope it helps!

    ----
    Enrique Comba Riepenhausen


    I always thought Smalltalk would beat Java, I just didn't know it
    would be called 'Ruby' when it did.
    -- Kent Beck
     
    Enrique Comba Riepenhausen, May 25, 2007
    #2
    1. Advertising

  3. Sy Ys Guest

    Enrique Comba Riepenhausen wrote:
    > Try the following:
    >
    > http = Net::HTTP.new('http://www.server.net')
    > response = http.post('/post.php', 'x=y', {'A'=>'1','B'=>'2','C' =>
    > '3'})
    >
    > puts response.body


    The x=y become POST variables and the {'A'=>'1' ...} drop out of the
    equation entirely.

    Actual code:

    http = Net:HTTP.new(domain.com)
    response = http.post(script.asp,'x=y',{'A'=>'1','B'=>'2'})
    content = response.body

    Perhaps I have misapplied it?

    Note: including http:// in the domain gives a connection error,
    strangely.

    --
    Posted via http://www.ruby-forum.com/.
     
    Sy Ys, May 25, 2007
    #3
  4. > The x=y become POST variables and the {'A'=>'1' ...} drop out of the
    > equation entirely.
    >
    > Actual code:
    >
    > http = Net:HTTP.new(domain.com)
    > response = http.post(script.asp,'x=y',{'A'=>'1','B'=>'2'})
    > content = response.body
    >
    > Perhaps I have misapplied it?
    >
    > Note: including http:// in the domain gives a connection error,
    > strangely.


    That is true, my typo...

    OOOOPsssss I'm sorry, my fault!

    Try this:
    http = Net::HTTP.new('domain.com')
    response = http.post('/script.asp', 'x=y')
    content = response.body

    The {} sets header fields if you take a closer look at the post
    request itself you will see them there.

    Cheers,
    ----
    Enrique Comba Riepenhausen


    I always thought Smalltalk would beat Java, I just didn't know it
    would be called 'Ruby' when it did.
    -- Kent Beck
     
    Enrique Comba Riepenhausen, May 25, 2007
    #4
  5. Sy Ys Guest

    Enrique Comba Riepenhausen wrote:
    > Try this:
    > http = Net::HTTP.new('domain.com')
    > response = http.post('/script.asp', 'x=y')
    > content = response.body
    >
    > The {} sets header fields if you take a closer look at the post
    > request itself you will see them there.


    Now you've confused me. The above example doesn't even contain the
    variables I want to send as POST. I need to send x=y as GET and a=1 as
    POST. Get it?

    In other words: I need to POST 'a=1' to 'script.asp?y=z'

    --
    Posted via http://www.ruby-forum.com/.
     
    Sy Ys, May 25, 2007
    #5
  6. On 25 May 2007, at 23:27, Sy Ys wrote:

    > Enrique Comba Riepenhausen wrote:
    >> Try this:
    >> http = Net::HTTP.new('domain.com')
    >> response = http.post('/script.asp', 'x=y')
    >> content = response.body
    >>
    >> The {} sets header fields if you take a closer look at the post
    >> request itself you will see them there.

    >
    > Now you've confused me. The above example doesn't even contain the
    > variables I want to send as POST. I need to send x=y as GET and a=1 as
    > POST. Get it?
    >
    > In other words: I need to POST 'a=1' to 'script.asp?y=z'


    Okay, then change the following:

    response = http.post('/script.asp?y=z', 'a=1')

    Sorry for the confusion.... ;)
     
    Enrique Comba Riepenhausen, May 25, 2007
    #6
  7. Sy Ys Guest

    Enrique Comba Riepenhausen wrote:
    > Sorry for the confusion.... ;)


    Now we're in business. =) Thanks for your wonderful help, buddy.

    --
    Posted via http://www.ruby-forum.com/.
     
    Sy Ys, May 25, 2007
    #7
  8. On 26 May 2007, at 00:05, Sy Ys wrote:

    > Enrique Comba Riepenhausen wrote:
    >> Sorry for the confusion.... ;)

    >
    > Now we're in business. =) Thanks for your wonderful help, buddy.
    >


    Anytime! ;)
     
    Enrique Comba Riepenhausen, May 25, 2007
    #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. Replies:
    6
    Views:
    6,048
    Tor Iver Wilhelmsen
    Aug 29, 2005
  2. James
    Replies:
    3
    Views:
    16,617
    Roedy Green
    Nov 25, 2005
  3. arnuld
    Replies:
    6
    Views:
    650
    arnuld
    Sep 5, 2007
  4. serge calderara

    HTTP GET vs HTTP POST protocol

    serge calderara, Nov 4, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    651
  5. n3d!m

    Http post and http get

    n3d!m, Jan 25, 2012, in forum: Python
    Replies:
    2
    Views:
    390
    n3d!m
    Feb 6, 2012
Loading...

Share This Page