Forum: Ruby on Rails RestClient and passing cookies

Ee6fb5cad9d1b269f1dcbb5a76c9d760?d=identicon&s=25 Moutalib Moutalib (jarh)
on 2014-08-12 22:36
Hello,

I had this post on the Ruby channel, but maybe I have more luck here :)

I'm struggling with the last piece of the puzzle here. Basically I want
to reuse a cookie that I capture from a previous RestClient.post()
login, the only problem is that the second RestClient.post will not
accept it. I have tried different methods to pass the cookie value to
the RestClient.post(), but with no success. Also tried the base64
encoding, also failed here. Can someone please advice?

Code:
==========================================
require 'json'
require 'rest-client'

url_login = 'http://192.168.1.51/cms/v1/login/login'
url_adduser= 'http://192.168.1.51/cms/v1/config/records'

payload_login = <<-JSON
{
  "login":
  {
   "username":"user-1",
   "password":"secret",

  }
}
JSON

payload_adduser= <<-JSON_ADDUSER
  {
    "adduser":
    {
     "name":"cms-users-2",
    }
  }
JSON_ADDUSER

initheader_login = {'Content-Type' =>
'application/x-www-form-urlencoded',:accept => :json}
initheader_add_user= {'Content-Type' => 'application/x-add-user'}

#the restexec_login goes well, I can see it login on the webserver (code
201)
restexec_login =
RestClient.post(url_login,payload_login,initheader_login)

#capture the cookie as a result of the execution of restexec_login
query_cookie = restexec_login.cookies
cookie_hash = query_cookie['SESSION_ID']
cookie = {:SESSION_ID => cookie_hash}

#the second post (below) will fail because of the missing cookie
variable
restexec_adduser=
RestClient.post(url_adduser,payload_adduser,initheader_add_user)

END CODE
==============

How can I pass the cookie in the second post method?

Thanks in advance!

Kind Regards,

iLias
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2014-08-14 10:37
(Received via mailing list)
On Tuesday, August 12, 2014 9:36:59 PM UTC+1, Ruby-Forum.com User wrote:
>
>
You need to set a header of the form

    Cookie: cookie_name=cookie_value; other_cookie_name=cookie_value

if your name/value have special characters in them you'll need to quote
them.

Fred
Ee6fb5cad9d1b269f1dcbb5a76c9d760?d=identicon&s=25 Moutalib Moutalib (jarh)
on 2014-08-16 17:31
Hello Fred,

Thanks for your reply! But the problem was that the cookie was expired
before I could reuse it.

Cheers,

iLias
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.