How to parse json accepted from android app in Rails - ruby-on-rails

how to parse json data sent from android app. Suppoce i have a method in my controller inside which i have to parse this json data.
Suppoce android app is able to send json to provided url. As i believe in order to get data from android app I have to provide some url to the android application.
The major question is where is that json is stored, do I access via parameters hash?

try this
in controller create one method and call it in before_action
e.g.
before_action :parse_json_data_to_params
def parse_json_data_to_params
params.merge!(ActiveSupport::JSON.decode(request.raw_post))
end
plese dont forget to write except block in front of bfore_action because it will raise an error for get requests.

Related

How to send HTTP GET request to Firebase Realtime DB on POSTMAN?

I have an android messaging app and I save all data to firebase realtime db. I want to get user data on c#.
First, I try to send GET method on POSTMAN with this url https://MY_PROJECT_ID.firebaseio.com/users/USER_ID
and returned data is :
I just want to get some JSON that includes my user's data. How to do it ?
You can click on the HTML button and change it to JSON.
just append .json at the end like this:
https://MY_PROJECT_ID.firebaseio.com/users/**USER_ID.json**

Random GET requests for POST URLs

In a rails app, I see crashes frequently (airbrakes) like below
AbstractController::ActionNotFound:The action '6826' could not be
found for MembersController
Investigating further, the URL is configured to receive only PUT or POST requests but instead receives GET. Doesn't seem to be action of bots or a smart user. Do browsers sometime send both GET and POST requests when a link is clicked, should it be handled in the server and redirected to corresponding pages?
A browser will only send a GET request when a link is clicked - unless there is JavaScript telling it otherwise. Your app should handle POST and PUT requests for data submission, GET requests for data retrieval, and DELETE requests for data transmission. There could be several different causes of that error, it could be because you are representing a POST or PUT route using a link or your users are typing the url into the address bar.

Process post request with rails

A little background info so you can know my overall goal:
I have a web app that uses rails and Devise for authentication. I also am making an iPhone app which I need to be able to send and retrieve data to/from this app. I've decided to make a custom token authentication system in order to handle this.
I want to be able to send a username and password in JSON format as a post request to my app, have the app get that username and password, then have the app reply with a token in an xml or JSON format.
How do I make an action that will get information from a post request (username and password) and process it then return something else?
I just recently attended a meetup that explained Helios, which sounds like what you are looking for.
Check out their docs. Also this Github is an example of helios in action.

Check if the client browser responds to URL with rails

I send emails to my iOS customers with links to our app. When they tap the link, I want to check if they have the app installed.
If the app is installed I want to open it. If not it will redirect to download the app in the App Store.
The URL I'm using to open the app is:
rva://store?uuid=EFBBD9FF-3976-4816-8B77-C1462C99E256
And the check I was using is:
def responds_to_url?(url)
response = Net::HTTP.get_response(URI(url)).code
if response == "200"
return true
else
false
end
end
The problem is that passing my URL above to URI() returns a Generic (capital G) URI object, whereas it should (if it were http) return an HTTP object.
How can I do a similar check with just a link (i.e. no javascript)?
I thought about passing the URL to Mobile Safari and seeing if it responds, but I don't know how to do that or even if it's possible.
You could do client browser detection with HTTP_USER_AGENT, I believe there is a gem for that: Rails Browser Detection Methods
What you describe is not possible. Your rails app will have very limited knowledge of the client—mostly in the form of the User Agent—and there is no way for your Rails app to query for more information while handling a request.

Facebook Open Graph Scraping

I've recently enabled the Facebook Open Graph stuff on my web app (so and so has just read this and that on here and there). Now I post the request to Facebook when a user posts something, as part of the page load in the controller. The problem is I receive the following error:
HTTP 500: Response body: {"error":{"type":"Exception","message":"Could
not retrieve data from URL."}}
My first thought is that the open request to load the page is blocking any FB scraping of OG information, as it seems after it's been cached I no longer receive this error.
Is this likely? If so, what's the best way to work around it?
WHY THIS HAPPENS:
I had this same problem today, and it is because your Koala script (assuming you're using koala - if not, you should try it out because it's great) sends its request to Facebook before your URL is up. This means that when Facebook registers the post, it comes to the URL you specified to pick up the meta tags. Unfortunately, the page itself hasn't been loaded yet, giving it a 500 error
HOW TO SOLVE IT:
Use the delayed_job gem to prevent your post call from occurring before the page loads, which allows facebook to scrape your metatags correctly.
FOR EXAMPLE:
def post_to_facebook([ACCESS_TOKEN])
graph = Koala::Facebook::API.new([ACCESS TOKEN])
graph.put_connections("me", "[APP NAMESPACE]:[ACTION]", :[OBJECT TYPE] => [OBJECT_URL])
end
handle_asynchronously :post_to_facebook

Resources