railslearner
railslearner

Reputation: 1821

RESTful RAILS and Android HttpRequests

I am currently working on building an Android app for my Rails backend.

I created api_tokens_controller to create an api_token when user logs in from Android using HttpPost and save that token both in Rails user table and Android shared preferences. I was able to verify this.

I am trying to send the api_token as params[:api_token] from my Android app with every subsequent request after initial log in and have Rails verify:

In User.rb:

def self.authenticate_with_api_token(api_token_from_mobile)
    user = find_by_api_token(api_token_from_mobile)
    if user.nil?
        return nil
    else
    return user
    end
end

if user exists, then send other data as JSONObjects.

I am running into trouble conceptually here. I am now trying to have the Android app go to ...:3000/users and HttpGet user data as JSONObjects. The problem is that I don't know how to send the api_token from Android app to Rails in HttpGet. I am assuming that since it is RESTful, my HttpRequests have to correspond to the way rails routes work.

Questions:

  1. Is it possible to send in JSONObject of params[:api_token] from android app to Rails in HttpGet?

  2. Do my HttpRequests have to correspond to the RESTful way Rails works or can I just only use HttpPost?

Thanks

Upvotes: 3

Views: 584

Answers (2)

louielouie
louielouie

Reputation: 14941

I'm a former Ruby on Rails developer myself.

I agree with Tom that you can just pass in the api_token as a parameter with an HTTP GET.

For your second question, I'd say that the RESTful way is to use all the HTTP Verbs for the different actions that you can take in the API. You could change your routes to use HTTP POST and then supply extra parameters to differentiate between say Create and Update (to emulate a PUT). However, I don't think that's the way forward.

However, the good news is that whether you use HttpClient or HttpUrlConnection in your Android app, you will have access to the different HTTP verbs you need. For HttpClient, it is actually different classes like HttpGet, HttpPost, HttpPut and HttpDelete which you then configure by supplying URL and other parameters. For HttpUrlConnection, you just call it like so:

setRequestMethod("PUT");

Here's the relevant documentation on HttpUrlConnection:

HttpURLConnection uses the GET method by default. It will use POST if setDoOutput(true) has been called. Other HTTP methods (OPTIONS, HEAD, PUT, DELETE and TRACE) can be used with setRequestMethod(String).

Upvotes: 0

Tom Livesey
Tom Livesey

Reputation: 411

If I understand you correctly, then you just want to add the API token to as a URL parameter if using GET, so something like:

...:3000/users.json?api_token=token

This will put the api_token in the params hash just the same as POST data would be. The RESTful way would be to use GET for retrieving data from the API and using POST to send data, i.e. changing data on your backend but if you are going to be returning a user with the request then GET is the correct method to be using anyway.

Upvotes: 1

Related Questions