I have a working rails app, which is a blog with comments and upvoting/downvoting. The mobile component is for users to log in/log out, view articles and upvote/downvote, not add articles or comments at this stage.
I have implemented the json login with the simple token auth gem and I can get the articles and their upvotes/downvotes which is all working but here is my problem:
How do I post the json values back? I have my json code working in Android to post or push json values but how do I build the correct string to push back? What is the correct parameters to put back?
Thanks very much.
This is from rails server log:
The html upvote which works:
Started PUT "/articles/5/like" for 137.147.172.125 at 2016-03-12 09:34:43 +0000
Cannot render console from 137.147.172.125! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ArticlesController#upvote as HTML
Parameters: {"authenticity_token"=>"4eC8xgC+1CBb51gKO7ZRzjL1BGOF6TYTnbqQcJ3evxvpmGcguYHBNYSn9v3LJMfoo3F29frntwzgyLoeI9oaLg==", "id"=>"5"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Article Load (0.1ms) SELECT "articles".* FROM "articles" WHERE "articles"."id" = ? ORDER BY "articles"."created_at" DESC LIMIT 1 [["id", 5]]
(0.3ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."voter_id" = ? AND "votes"."voter_type" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 5], ["votable_type", "Article"], ["voter_id", 1], ["voter_type", "User"]]
(0.2ms) begin transaction
SQL (0.6ms) INSERT INTO "votes" ("votable_id", "votable_type", "voter_id", "voter_type", "vote_flag", "vote_weight", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["votable_id", 5], ["votable_type", "Article"], ["voter_id", 1], ["voter_type", "User"], ["vote_flag", "t"], ["vote_weight", 1], ["created_at", "2016-03-12 09:34:43.393685"], ["updated_at", "2016-03-12 09:34:43.393685"]]
(11.4ms) commit transaction
Redirected to https://completerubyonrailscourse-adam1st.c9users.io/articles/5
Completed 302 Found in 187ms (ActiveRecord: 13.2ms)
This is when my android code attempts to push the json data back to the rails server:
Started PUT "/articles/5/like" for 49.199.131.149 at 2016-03-12 10:20:16 +0000
Cannot render console from 49.199.131.149! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ArticlesController#upvote as JSON
Parameters: {"votes"=>{"votable_id"=>"5", "votable_type"=>"Article", "voter_id"=>"1", "voter_type"=>"User", "vote_flag"=>"t", "vote_weight"=>"1", "created_at"=>"2016-03-12 21:20:13.679000", "updated_at"=>"2016-03-12 21:20:13.679000"}, "id"=>"5", "article"=>{}}
Can't verify CSRF token authenticity
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms)
So how do I get this into the json data?
Parameters:{"authenticity_token"=>"4eC8xgC+1CBb51gKO7ZRzjL1BGOF6TYTnbqQcJ3evxvpmGcguYHBNYSn9v3LJMfoo3F29frntwzgyLoeI9oaLg==", "id"=>"5"}
From the articles controller:
before_action :authenticate_user!, :except => [:index, :show]
before_filter :set_article, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
.
.
.
def upvote
@article.upvote_by current_user
flash[:success] = "Successfully liked"
respond_to do |format|
format.html {redirect_to :back }
format.json { render json: { count: @article.liked_count } }
end
end
def downvote
@article.downvote_by current_user
flash[:success] = "Successfully disliked"
respond_to do |format|
format.html {redirect_to :back }
format.json { render json: { count: @article.disliked_count } }
end
end
From the show.html.erb that concerns the voting:
<%= link_to like_article_path(@article), method: :put, class: 'voting' do %>
<i class="fa fa-thumbs-up"></i>
<%= @article.get_upvotes.size %>
<% end %>
<%= link_to dislike_article_path(@article), method: :put, class: 'voting' do %>
<i class="fa fa-thumbs-down"></i>
<%= @article.get_downvotes.size %>
<% end %>
Android PUT code (some values hard coded for testing):
private class VoteTask extends UrlJsonAsyncTask {
public VoteTask(Context context) {
super(context);
}
@Override
protected JSONObject doInBackground(String... urls) {
HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(urls[0]);
JSONObject holder = new JSONObject();
JSONObject voteObj = new JSONObject();
String response = null;
JSONObject json = new JSONObject();
try {
try {
// setup the returned values in case
// something goes wrong
json.put("success", false);
json.put("info", "Something went wrong. Retry!");
// add the user email and password to
// the params
voteObj.put("votable_id",articleId);
voteObj.put("votable_type", "Article");
voteObj.put("voter_id", "1");
voteObj.put("voter_type", "User");
voteObj.put("vote_flag", "t");
voteObj.put("vote_weight", "1");
voteObj.put("created_at", strDate);
voteObj.put("updated_at", strDate);
holder.put("votes", voteObj);
StringEntity se = new StringEntity(holder.toString());
put.setEntity(se);
// setup the request headers
put.setHeader("Accept", "application/json");
put.setHeader("Content-Type", "application/json");
response = String.valueOf(client.execute(put));
json = new JSONObject(response);
} catch (IOException e) {
e.printStackTrace();
Log.e("IO", "" + e);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON", "" + e);
}
return json;
}
I went to the git of Simple token authentication https://github.com/gonzalo-bulnes/simple_token_authentication and saw this.
Authentication Method 2: Request Headers
You can also use request headers (which may be simpler when authenticating against an API):
X-User-Email alice@example.com
X-User-Token 1G8_s7P-V-4MGojaKD7a
so I put that in my vote task in the headers before performing my vote and it worked.
private class VoteTask extends UrlJsonAsyncTask {
public VoteTask(Context context) {
super(context);
}
@Override
protected JSONObject doInBackground(String... urls) {
DefaultHttpClient webClient = new DefaultHttpClient();
HttpPut put = new HttpPut(urls[0]);
JSONObject holder = new JSONObject();
JSONObject voteObj = new JSONObject();
String response = null;
JSONObject json = new JSONObject();
try {
try {
// setup the returned values in case
// something goes wrong
json.put("success", false);
json.put("info", "Something went wrong. Retry!");
//get stored values to prove the user is authorised
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String id = sharedPreferences.getString("userId", "default value");
String authToken = sharedPreferences.getString("AuthToken", "default value");
String email = sharedPreferences.getString("email", "default value");
voteObj.put("votable_id",articleId);
voteObj.put("votable_type", "Article");
voteObj.put("voter_id", id);
voteObj.put("voter_type", "User");
voteObj.put("vote_flag", voteFlag);
voteObj.put("vote_weight", "1");
holder.put("votes", voteObj);
holder.put("article", articleId);
StringEntity se = new StringEntity(holder.toString());
put.setEntity(se);
// setup the request headers
put.setHeader("Accept", "application/json");
put.setHeader("Content-Type", "application/json");
// add email and auth token to validate
put.setHeader("X-User-Email", email);
put.setHeader("X-User-Token", authToken);
//response = webClient.execute(put);
//json = new JSONObject(response);
response = String.valueOf(webClient.execute(put));
json = new JSONObject(response);
} catch (HttpResponseException e) {
e.printStackTrace();
Log.e("ClientProtocol", "" + e);
json.put("info", "Email and/or password are invalid. Retry!");
} catch (IOException e) {
e.printStackTrace();
Log.e("IO", "" + e);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON", "" + e);
}
return json;
}