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

S

subsume

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
 
E

Enrique Comba Riepenhausen

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

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
(e-mail address removed)

I always thought Smalltalk would beat Java, I just didn't know it
would be called 'Ruby' when it did.
-- Kent Beck
 
S

Sy Ys

Enrique said:
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.
 
E

Enrique Comba Riepenhausen

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
(e-mail address removed)

I always thought Smalltalk would beat Java, I just didn't know it
would be called 'Ruby' when it did.
-- Kent Beck
 
S

Sy Ys

Enrique said:
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'
 
E

Enrique Comba Riepenhausen

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.... ;)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top