[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Net::HTTP post

Mark Volkmann

1/23/2006 3:00:00 AM

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...,
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'
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

--
R. Mark Volkmann
Partner, Object Computing, Inc.


7 Answers

james_b

1/23/2006 5:21:00 AM

0

Mark Volkmann 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...,
> 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"


Stephen Hildrey

1/23/2006 2:19:00 PM

0

Mark Volkmann 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

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
--
Stephen Hildrey
E-mail: steve@uptime.org.uk / Tel: +442071931337
Jabber: steve@jabber.earth.li / MSN: foo@hotmail.co.uk
PGP: http://www.uptime.org.uk/st... (0x3A61C909)

Mark Volkmann

1/23/2006 2:32:00 PM

0

On 1/22/06, James Britt <james_b@neurogami.com> wrote:
> Mark Volkmann 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...,
> > 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.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


Matthias Ludwig

1/23/2006 2:50:00 PM

0

> 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'
res, data = http.post '/index/login/',
'email=matthias%40kl-mailer.de&password=xxx'
if res.code == 200
puts data

regards,
Matthias


Stephen Hildrey

1/23/2006 2:55:00 PM

0

Mark Volkmann 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
--
Stephen Hildrey
E-mail: steve@uptime.org.uk / Tel: +442071931337
Jabber: steve@jabber.earth.li / MSN: foo@hotmail.co.uk
PGP: http://www.uptime.org.uk/st... (0x3A61C909)

Stephen Hildrey

1/23/2006 2:59:00 PM

0

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
--
Stephen Hildrey
E-mail: steve@uptime.org.uk / Tel: +442071931337
Jabber: steve@jabber.earth.li / MSN: foo@hotmail.co.uk
PGP: http://www.uptime.org.uk/st... (0x3A61C909)

Mark Volkmann

1/23/2006 3:16:00 PM

0

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.

On 1/23/06, Matthias Ludwig <matthias@kl-mailer.de> wrote:
> > 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'
> res, data = http.post '/index/login/',
> 'email=matthias%40kl-mailer.de&password=xxx'
> if res.code == 200
> puts data
>
> regards,
> Matthias
>
>


--
R. Mark Volkmann
Partner, Object Computing, Inc.