Alberto
Alberto

Reputation: 467

Sinatra with sessions using Rack::Session::Pool

I am having a problem with Sinatra using Rack::Session::Pool for storing session information. What I want to do is to 'post' some data by appending information to the session hash using 'POST /dataset', and then retrieve it by using 'GET /dataset' and returning the content of 'session' hash. When I try to return the value though, the 'session' hash does not contain the :message key

require 'sinatra/base'

class Trial < Sinatra::Base
  use Rack::Session::Pool

  post '/dataset' do
    session[:message] = params[:data]
  end

  get '/dataset' do
    session[:message]
  end

end

Trial.run!

I know this looks trivial, but still I can't get it to work...

Upvotes: 1

Views: 1383

Answers (2)

matt
matt

Reputation: 79723

Even though you are using Rack::Session::Pool rather than the default cookie based session storage, you still need to use cookies in the requests. The session data is stored in memory on the server, but the session id needs to be passed with each request in a cookie.

response1 = RestClient.post 'localhost:4567/dataset', {:data => '123'}

response2 = RestClient.get 'localhost:4567/dataset', :cookies => response1.cookies

puts response2 #=> 123

Upvotes: 2

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

Try to

enable :sessions

More info about configuration.

Upvotes: 0

Related Questions