RESTful RAILS and Android HttpRequests

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

I created an api_tokens_controller to create an api_token when a user logs in from Android using HttpPost and stores this token in both the Rails users table and Android general settings. I was able to verify this.

I am trying to send api_token as params [: api_token] from my android app with every subsequent request after initial login and check Rails:

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
    return user


if user exists, then send other data as JSONObjects.

I ran into a problem conceptually here. Now I am trying to run android app ...: 3000 / users and HttpGet user data as JSONObjects. The problem is I don't know how to send the api_token from Android app to Rails in HttpGet. I am guessing that since it is RESTful my HttpRequests should be consistent with how the route routes work.


  • Is it possible to send to JSONObject parameters [: api_token] from android app in Rails to HttpGet?

  • Do my HttpRequests have to match the RESTful Rails path, or can I only use HttpPost?



source to share

2 answers

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

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

This will cause the api_token in the params hashes to be the same as the POST data. The RESTful method would have to use GET to fetch data from the API and use POST to send the data i.e. Changes to data on your server, but if you are going to return a user with a request, GET is the correct method to use anyway.



I am a former Ruby on Rails developer.

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

For your second question, I would say that it is a RESTful way to use all HTTP verbs for the various actions you can take in the API. You can change your routes to use HTTP POST and then provide additional parameters to parse between Create and Update (to emulate PUT). However, I don't think the way forward.

However, the good news is whether you are using HttpClient or HttpUrlConnection in your Android app, you will have access to the various HTTP verbs you need. For HttpClient, they are actually different classes like HttpGet, HttpPost, HttpPut, and HttpDelete, which are then configured by providing the URL and other parameters. For HttpUrlConnection, you simply call it like this:



Here's the relevant documentation on HttpUrlConnection :

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



All Articles