Http request

Hi!
I was trying to do an http request using gem httpclient.
First at all i have to login with user and pass, if the user and pass is
correct the server send me a cookie that i have to use to other request
My code is:

c = HTTPClient.new
c.debug_dev = STDOUT
c.set_cookie_store(‘cookie.dat’)
request = {“user”=>“user”,“pass”=>“pass”}

uri = “http://localhost/index.php
res = c.post(uri,request,:follow_redirect => true)
puts “SALIDA: \n\n”+res.content
c.save_cookie_store

If i look the console i can see that all is correct but i can’t see
enything when i puts res.conten or res.body. Why?
And if i have to do more request’s how do i pass the cookie to the
server?
Thank’s

Mariano Alejandro wrote in post #1087722:

Hi!
I was trying to do an http request using gem httpclient.
First at all i have to login with user and pass, if the user and pass is
correct the server send me a cookie that i have to use to other request
My code is:

c = HTTPClient.new
c.debug_dev = STDOUT
c.set_cookie_store(‘cookie.dat’)
request = {“user”=>“user”,“pass”=>“pass”}

uri = “http://localhost/index.php
res = c.post(uri,request,:follow_redirect => true)
puts “SALIDA: \n\n”+res.content
c.save_cookie_store

If i look the console i can see that all is correct but i can’t see
enything when i puts res.conten or res.body. Why?

Your code works fine for me. If you write web programs,
then you need to setup a server on your computer, so that
you can test your web programs. That will enable you to write simple
cgi programs to echo back some information, like the
name/value pairs sent to the server in a query string(GET) or in the
body of the request(POST); or set a cookie and see if it comes back in a
second request.

If the first line of the response is “HTTP/1.1 200 OK” and there is no
resp.content, then the server didn’t send anything back to your program.

And if i have to do more request’s how do i pass the cookie to the
server?
Thank’s

httpclient automatically handles cookies for you–you don’t have to do
anything. httpclient will send back any cookies that were set by the
server
while your program is running. If you want to save the cookies for the
next time you run your program, that’s when you use set_cookie_store()
and save_cookie_store().

Also, you should not be using :follow_redirect with a POST request:

  • (Object) post(uri, *args, &block)

Sends POST request to the specified URL. See request for arguments. You
should not depend on :follow_redirect => true for POST method. It sends
the same POST method to the new location which is prohibited in HTTP
spec.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs