Forum: Ruby REST-Client and passing the cookie

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

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
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2014-08-12 22:36
Reading the source
https://github.com/rest-client/rest-client/blob/ma...,
the syntax for setting cookies seems to be

> initheader = {...}
> initheader[:cookies] = { 'key1' => 'val1', 'key2' => 'val2' , ...}
> RestClient.post(url,payload,initheader)

Does it work? Otherwise, you could always just set the header directly
http://en.wikipedia.org/wiki/HTTP_cookie#Implementation

This should work as well
> RestClient::Request.execute(:method => :post, :url => url,
> :payload => payload, :headers => headers,
> :cookies => {'key1' => 'val1',...}) {...}
Ee6fb5cad9d1b269f1dcbb5a76c9d760?d=identicon&s=25 Moutalib Moutalib (jarh)
on 2014-08-13 12:25
Hi Dansei,

Thanks for your reply!

Unfortunately I get a 400  Bad Request error(RestClient::BadRequest).
I'm going to find out if I can use basic authentication instead of
cookie.

Kind Regards,

ilias
4a65f01f7ece0b720bdb0de3c3db089e?d=identicon&s=25 Dansei Yuuki (blutorange)
on 2014-08-13 17:48
You could also try curl, which provides a verbose mode. Its interface is
similar.
http://www.ruby-doc.org/gems/docs/v/vim-jar-0.1.2/...

> require 'curl'
> c = Curl::Easy.http_post(url,payload) do |curl|
>     curl.enable_cookies = true
>     curl.cookies = 'key1=val1;key2=val2;...;'
>
>     curl.headers['Content-Type'] = 'application/x-www-form-urlencoded'
>     ...
>
>     curl.verbose = true
> end
>
> puts c.response_code # eg 201
> puts c.header_str # response header
> puts c.body_str # response body

Setting verbose to true will print the http post request to your
terminal, so that you can compare it with what the server expects. Also,
check whether you need 'Cookie: ' or 'Set-Cookie: '.
Ee6fb5cad9d1b269f1dcbb5a76c9d760?d=identicon&s=25 Moutalib Moutalib (jarh)
on 2014-08-15 12:34
Hi Dansei,

Thanks for sticking around, curl show me indeed more information with
the verbose mode. The cookie stuff didn't work eventually, I think duo
the fact that the cookies was expired or so.

I found within the API another way to authenticate, with headers, so
this is working oke for now :)

Thanks for your support, it was very helpful!

Kind Regards,

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.