REST-Client and passing the cookie

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

Reading the source
rest-client/lib/restclient/request.rb at master · rest-client/rest-client · GitHub,
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

This should work as well

RestClient::Request.execute(:method => :post, :url => url,
:payload => payload, :headers => headers,
:cookies => {‘key1’ => ‘val1’,…}) {…}

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

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/Curl/Easy.html

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: '.

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 :slight_smile:

Thanks for your support, it was very helpful!

Kind Regards,

iLias