Search code examples
ruby-on-railsrubymotion

Rubymotion: GET Request using JSON


I am new to iOS programming and using Rubymotion to build my first application. In my Rubymotion app I POST to a webserver (an app built using RoR) to authenticate a user wanting to Login and am using the BubbleWrap gem for RubyMotion https://github.com/rubymotion/BubbleWrap/blob/master/motion/http.rb

def login(sender)
  payload = {email: @email, password: @password}
  BubbleWrap::HTTP.post("http://example.com/sessions", {payload:   payload}) do |response|
  @authCookie = response.headers['Set-Cookie']
 end 
end

Now once I receive successful authentication I move onto to receive JSON data from the web application using the following code:

BubbleWrap::HTTP.get("http://example.com/events.json", {cookie: @authCookie, :headers=>{"Content-Type"=>'json'} }) do |response|
 puts response.body #testing the response
end

For some reason, the authentication token received from the POST request is not being correctly passed on by the GET request. I know this because in the web app if authentification fails it redirects to the login page and that's the response (HTML code of the Login page) am receiving from the GET request.

Authentication check on the Web App:

   def session_check
    if session[:bizid].nil?
     redirect_to login_url
     flash[:notice] = "Please login to view your account!"
     end 
   end

Additionally, on the web app this authentication token is set by the following method:

def create
    current_biz = Bizname.find_by_email(params[:email])
        if current_biz && current_biz.authenticate(params[:password])
            session[:bizid] = current_biz.id
            flash[:notice] = 'Login Successful!'

            if current_biz.events.empty?
                redirect_to getsetup_url
            else
                redirect_to  account_summary_url
            end

        else
            flash[:notice] = 'Incorrect Email or Password.'
            redirect_to login_url   
        end 
    end

Any ideas of what I might be doing wrong here?

Thanks in advance!


Solution

  • You just need to pass the cookie in as a header too. For example:

    BubbleWrap::HTTP.get("http://example.com/events.json", :headers=> {"Content-Type"=>'json', "Cookie" => @authCookie}) do |response|
      puts response.body #testing the response
    end