# Net::HTTP post

**URL:** <https://www.ruby-forum.com/t/net-http-post/96076>\
**Category:** Ruby\
**Created:** [January 23, 2006, 4:01am UTC](https://www.ruby-forum.com/t/net-http-post/96076 "2006-01-23T04:01:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 23, 2006, 4:01am UTC](https://www.ruby-forum.com/t/net-http-post/96076/1 "2006-01-23T04:01:05Z")

</div>

Can someone point me to an example of sending a POST request using  
Net::HTTP?

I found one at  
[http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html),  
but this use the method post\_data which, when I look at the source in  
http.rb, is not a method of Net::HTTP. Maybe it’s outdated  
documentation.

This is what I’ve tried. I suspect I’m not passing the form data  
correctly. The response is a Net::HTTPBadRequest.

* * *

require ‘net/http’

host = ‘[www.runningbuzz.com](http://www.runningbuzz.com)’  
Net::HTTP.start(host) do |http|

data = {  
‘CalcWhat’ =\> ‘2’,  
‘timeH’ =\> ‘2’,  
‘timeM’ =\> ‘57’,  
‘timeS’ =\> ‘17’,  
‘distance’ =\> ‘26.2’,  
‘optDist’ =\> ‘miles’,  
‘optPace’ =\> ‘miles’  
}

response = http.post(‘pace\_calculator.htm’, data)

puts response.body  
end

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 23, 2006, 6:22am UTC](https://www.ruby-forum.com/t/net-http-post/96076/2 "2006-01-23T06:22:00Z")

</div>

Mark V. wrote:

> Can someone point me to an example of sending a POST request using Net::HTTP?
> 
> I found one at [http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html),  
> but this use the method post\_data which, when I look at the source in  
> http.rb, is not a method of Net::HTTP. Maybe it’s outdated  
> documentation.

What is your version of Ruby?

## James

“Blanket statements are over-rated”

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 23, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/net-http-post/96076/3 "2006-01-23T15:35:06Z")

</div>

On 1/22/06, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mark V. wrote:
> 
> > Can someone point me to an example of sending a POST request using Net::HTTP?
> > 
> > I found one at [http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html](http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTP.html),  
> > but this use the method post\_data which, when I look at the source in  
> > http.rb, is not a method of Net::HTTP. Maybe it’s outdated  
> > documentation.
> 
> What is your version of Ruby?

It’s 1.8.2 and I’m running under XP.

I think my problem is that I need to pass a String as the second  
parameter to HTTP.post. I don’t know how to format it though. Should  
it look like this?

“name1=value1&name2=value2”

This format isn’t working for me.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 23, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/net-http-post/96076/4 "2006-01-23T16:16:23Z")

</div>

I figured out why it wasn’t working. There were two problems.

1. The file path I passed as the first parameter to the post method  
didn’t start with a /.
2. The HTML form I was posting to only supports GET requests, not POST.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 24, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/net-http-post/96076/5 "2006-01-24T17:46:45Z")

</div>

Mark V. wrote:

> This is what I’ve tried. I suspect I’m not passing the form data  
> correctly. The response is a Net::HTTPBadRequest.

There seems to be a couple of issues:

1. Looking at the URL you specified, I’m not sure you can POST to it; as  
far as I can tell, the calculator there is implemented client-side using  
the Javascript contained in /pacecalc.js. This doesn’t necessarily mean  
it can’t accept POST requests, but it’s worth checking that first.

2. When I run your code, I see the following HTTP:

POST pace\_calculator.htm HTTP/1.1  
Accept: _/_  
Content-Type: application/x-www-form-urlencoded  
Content-Length: 7  
Host: [www.runningbuzz.com](http://www.runningbuzz.com)

timeM57CalcWhat2timeH2optPacemilestimeS17optDistmilesdistance26.2

That’s clearly wrong. If you look at the docs for Net/HTTP you’ll see  
that “data” must be a string, not a hash. So rather than a hash, you  
want a string like ‘CalcWhat=2&timeH=2’ etc. Check Net::HTTP#post.

Hope that helps,  
Cheers,  
Steve

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 24, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/net-http-post/96076/6 "2006-01-24T17:49:47Z")

</div>

Mark V. wrote:

> I think my problem is that I need to pass a String as the second  
> parameter to HTTP.post. I don’t know how to format it though. Should  
> it look like this?
> 
> “name1=value1&name2=value2”
> 
> This format isn’t working for me.

Yep - see my other post.

You can convert from your ‘data’ hash to a suitable string using  
something like:

data.to\_a.collect {|k,v| k.to\_s+’=’+v}.join(’&’)

HTH,  
Steve

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 24, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/net-http-post/96076/7 "2006-01-24T17:52:48Z")

</div>

Stephen Hildrey wrote:

> data.to\_a.collect {|k,v| k.to\_s+’=’+v}.join(’&’)

Oops, poor form to reply to self, etc…

I didn’t mean to have the to\_a in that above!

Steve

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 23, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/net-http-post/96076/8 "2006-01-23T15:50:15Z")

</div>

> It’s 1.8.2 and I’m running under XP.
> 
> I think my problem is that I need to pass a String as the second  
> parameter to HTTP.post. I don’t know how to format it though. Should  
> it look like this?
> 
> “name1=value1&name2=value2”

I think, your code should work…  
This is working fine (for me):

http = Net::HTTP.new ‘[www.xxx.de](http://www.xxx.de)’  
res, data = http.post ‘/index/login/’,  
‘email=matthias%[40kl-mailer.de](http://40kl-mailer.de)&password=xxx’  
if res.code == 200  
puts data

regards,  
Matthias
