Forum: Ruby net/http post request with nested hash data ?

Posted by AleiPhoenix (A.K.A Areverie) (Guest)
on 2012-12-16 15:54
(Received via mailing list)
Hi,

I'm currently working on a script doing login function with a website.

The login action requires a hash-like parameters as

login[account] = "foobar@example.com"
login[passwd] = "whatever"

setting params in net/http like

require "net/http"
require "net/https"

https = Net::HTTP.new("passport.115.com", 443)
https.use_ssl = true
req = Net::HTTP::Post.new("/?ac=login")
req["login[account]"] = "foobar@example.com"
req["login[passwd]"] = "whatever"
res = https.request(req)

p res.to_hash

won't work, looks like the params weren't passed properly.

and with

req.set_form_data({"login" => {"account" => "foobar@example.com", 
"login"
=> "passwd"}})

has no luck either.



but with

req.set_form_data({"login[account]" => "foobar@example.com",
"login[passwd]" => "whatever"})

doing the job.

Any ideas?

Thanks

--
Silence is golden.

twitter: @areverie
wikipedia: AleiPhoenix
blog: weblog.areverie.org
wiki: wiki.areverie.org
Posted by Eric Hodel (Guest)
on 2012-12-16 23:23
(Received via mailing list)
On Dec 16, 2012, at 6:53 AM, "AleiPhoenix (A.K.A Areverie)" 
<aleiphoenix@gmail.com> wrote:
> require "net/https"
>
> https = Net::HTTP.new("passport.115.com", 443)
> https.use_ssl = true
> req = Net::HTTP::Post.new("/?ac=login")

Net::HTTPRequest#[] sets request headers, not form data.

> req["login[account]"] = "foobar@example.com"
> req["login[passwd]"] = "whatever"
> res = https.request(req)
>
> p res.to_hash
>
> won't work, looks like the params weren't passed properly.
>
> and with

This is not how the web browser is set up to send the parameters.

> req.set_form_data({"login" => {"account" => "foobar@example.com", "login" => 
"passwd"}})
>
> has no luck either.

This is how the web browser sends the parameters.

> but with
>
> req.set_form_data({"login[account]" => "foobar@example.com", "login[passwd]" => 
"whatever"})
>
> doing the job.
>
> Any ideas?

Use this last one.
Posted by AleiPhoenix (A.K.A Areverie) (Guest)
on 2012-12-18 06:47
(Received via mailing list)
Got it, thanks.


On Mon, Dec 17, 2012 at 6:20 AM, Eric Hodel <drbrain@segment7.net> 
wrote:

> >
> > req["login[passwd]"] = "whatever"
> > req.set_form_data({"login" => {"account" => "foobar@example.com",
> >
> > doing the job.
> >
> > Any ideas?
>
> Use this last one.
>



--
Silence is golden.

twitter: @areverie
wikipedia: AleiPhoenix
blog: weblog.areverie.org
wiki: wiki.areverie.org
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.