Forum: Ruby REST-Client and passing the cookie

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
04cc985047618eb6d179494cc98e0070?d=identicon&s=25 Moutalib A. (jarh)
on 2014-08-12 13:19

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

require 'json'
require 'rest-client'

url_login = ''
url_adduser= ''

payload_login = <<-JSON


payload_adduser= <<-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
restexec_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


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

Thanks in advance!

Kind Regards,

C0d90cf1f2356ec0c2c9b40f28c6d2c2?d=identicon&s=25 Dansei Y. (blutorange)
on 2014-08-12 22:36
Reading the source,
the syntax for setting cookies seems to be

> initheader = {...}
> initheader[:cookies] = { 'key1' => 'val1', 'key2' => 'val2' , ...}

Does it work? Otherwise, you could always just set the header directly

This should work as well
> RestClient::Request.execute(:method => :post, :url => url,
> :payload => payload, :headers => headers,
> :cookies => {'key1' => 'val1',...}) {...}
04cc985047618eb6d179494cc98e0070?d=identicon&s=25 Moutalib A. (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

Kind Regards,

C0d90cf1f2356ec0c2c9b40f28c6d2c2?d=identicon&s=25 Dansei Y. (blutorange)
on 2014-08-13 17:48
You could also try curl, which provides a verbose mode. Its interface is

> 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: '.
04cc985047618eb6d179494cc98e0070?d=identicon&s=25 Moutalib A. (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,

This topic is locked and can not be replied to.