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

**URL:** <https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116>\
**Category:** Ruby\
**Created:** [May 25, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116 "2007-05-25T22:15:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/1 "2007-05-25T22:15:00Z")

</div>

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](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!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 10:28pm UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/2 "2007-05-25T22:28:46Z")

</div>

On 25 May 2007, at 22:15, removed\_email\_address@domain.invalid wrote:

> Net::HTTP.post\_form(URI.parse(‘[http://www.server.net/post.php?x=y](http://www.server.net/post.php?x=y)’),  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Try the following:

http = Net::HTTP.new(‘[http://www.server.net](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 R.  
removed\_email\_address@domain.invalid

I always thought Smalltalk would beat Java, I just didn’t know it  
would be called ‘Ruby’ when it did.  
– Kent Beck

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/3 "2007-05-25T23:01:49Z")

</div>

Enrique Comba R. wrote:

> Try the following:
> 
> http = Net::HTTP.new(‘[http://www.server.net](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](http://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.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/4 "2007-05-25T23:16:50Z")

</div>

> 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](http://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 R.  
removed\_email\_address@domain.invalid

I always thought Smalltalk would beat Java, I just didn’t know it  
would be called ‘Ruby’ when it did.  
– Kent Beck

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 11:27pm UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/5 "2007-05-25T23:27:05Z")

</div>

Enrique Comba R. wrote:

> Try this:  
> http = Net::HTTP.new(‘[domain.com](http://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’

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 26, 2007, 12:05am UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/6 "2007-05-26T00:05:11Z")

</div>

Enrique Comba R. wrote:

> Sorry for the confusion… 😉

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 25, 2007, 11:37pm UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/7 "2007-05-25T23:37:52Z")

</div>

On 25 May 2007, at 23:27, Sy Ys wrote:

> 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… 😉

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 26, 2007, 12:32am UTC](https://www.ruby-forum.com/t/net-http-post-and-get-in-the-same-request/105116/8 "2007-05-26T00:32:57Z")

</div>

On 26 May 2007, at 00:05, Sy Ys wrote:

> Enrique Comba R. wrote:
> 
> > Sorry for the confusion… 😉
> 
> Now we’re in business. =) Thanks for your wonderful help, buddy.

Anytime! 😉
