POST via json in Ruby on Rails - ruby-on-rails

I'm trying to create record in db via json. The problem is that i don't know how to compose http request in URL bar:
It should be something like:
http://localhost:3000/addnewpost.json?content=sometexthere
Or this is not correct?

It's not correct. Creating a record should be a POST command, not a GET. As such, the data content should go in the POST headers, not in the URL.
Are you sending this POST directly from Ruby? (if so, you'll want to look at Net::HTTP or some other ruby HTTP client). Show us some code and we'll help you improve it.

Related

How to get and display API like the facebook graph in rails

I've went through most of the beginner rails books and I want to try creating something on my own. For a start, I just want to create a few pages in rails that will get from Facebook's api and display something like username, likes, post. I've searched around and couldn't find an answer. My friend recommended that I use a gem called fb_graph, but reviewing the documentation, I have no clue how to use it.
Thanks stackoverflow!
You can query the Graph API directly. The responses will be in JSON which you can then parse into Ruby hash. See the Facebook Documentation for more details on to call specific and sample JSON responses. So here is general guide how to you can start playing around with this:-
Make a API Call to Facebook using Graph API
Explorer. Keep playing around with api until you get a response you want. Note the request params you passed to get that response & JSON you received from facebook.
Send a HTTP request containing those same params in rails using koala, 'fb_graph' or just plain NET:HTTP. It doesn't matter what client you use to sent the request, as long as you send same params as in step1, you will get that familar JSON as response.
Now, once you have the json, just have to parse it. Provided, if the client library is not already doing that as most fb gems will turn JSON into ruby objects/hashes. But if they don't, then you have to do it manually, its something like JSON.parse('JSON_RESPONSE_AS_STRING_GOES_HERE'). after this you will have a plain-old ruby hash which is you can save to db, display in view or whatever you want to do.
Hope it helps

Sending json from one Rails app to another

My question is simple here. I want to send json data from one Rails app to other Rails app via POST. How do I achieve that?
You can find a complete architectural example of how to do this with ActiveResource here
If you just need a simple call, I'd suggest to use REST-client for the calling side.
The provider side is pretty standard. Just expose an action that responds to JSON posts. Let me know if you need more details.
Have one Rails app accept JSON via a POST action in a controller, and have the other Rails app submit the JSON via an HTTP library like rest-client.

Is it possible to discover the expected params that is required in order to POST successfully?

Here is the scope of the issue Im dealing with...
I just took over an existing rails app that has a very complex business logic. The app is being used as a service. Json in, Json out.
Some of the resources expecting to find an abstract model in params hash and then via meta programming it expects to find the right object properties provided.
Giving that i have no docs, or unit tests, etc I'm finding it very difficult to figure out what to include in the params hash so the post/put operatins succeeds.
Are you able to boot it up with rails server and use the service? If so, you can just track what params it actually uses by adding <%= debug(params) if Rails.env.development? %> to your layout.
I suspect you don't know how to POST web requests to your JSON API. Try curl:
curl -fname=ariejan -fother_params=1 -fanother_param=#filename http://localhost:3000/test.json
Add the -f option automatically makes curl do a POST request. You'll get back the server response in JSON as well.

Sending XML over HTTP with Rails

I am dealing with a third-party api here and I need to send HTTP Post request represented in XML. How should I go about doing this in Rails? Which library/method if any will allow me to do this?
Try net/http package, in particular post method. There're examples too.
As to xml part, you can send any data you want as long as it's string.
A good starting point would be Net::HTTP library: http://stdlib.rubyonrails.org/libdoc/net/http/rdoc/index.html

Pubsubhubbub on Rails. How to extract the raw POST body contents from the POST request?

I am having trouble setting up a pubsub enabled subscriber app using rails. I have currently subscribed to the open hub pubsubhubbub.appspot.com and am receiving pings to my application's endpoint. (as of now i have created a counter which increments everytime the end point is pinged). But i am not able to understand as to how to extract the raw POST body contents from the POST. I am new to pubsub and am eager to experiment with it. I came across this blog post but it is not language specific.
Source: Joseph Smarr: Implementing PubSubHubbub subscriber support: A step-by-step guide. http://josephsmarr.com/2010/03/01/implementing-pubsubhubbub-subscriber-support-a-step-by-step-guide/
Now you’re ready for the
pay-out–magically receiving pings from
the ether every time the blog you’ve
subscribed to has new content! You’ll
receive inbound requests to your
specified callback URL without any
additional query parameters added
(i.e. you’ll know it’s a ping and not
a verification because there won’t be
any hub.mode parameter included).
Instead, the new entries of the
subscribed feed will be included
directly in the POST body of the
request, with a request Content-Type
of application/atom+xml for ATOM feeds
and application/rss+xml for RSS
feeds. Depending on your programming
language of choice, you’ll need to
figure out how to extract the raw POST
body contents. For instance, in PHP
you would fopen the special filename
php://input to read it.
Any help would be greatly appreciated.
You didn't say but I'm assuming you are running Rails 3.x?
To get the raw POST body you simply use request.raw_post in your controller. This will give you a long string that looks like a request parameters string: some_var=something&something_else=something_else... which you can then parse to get at what you want.
However, look at you development logs for an incoming request and see if the params hash isn't a better option for you. The service should post the data under some variable name, such as some_var above, and the params hash will hold an params[:some_var] containing only that data. No need for you to dig it out on your own in other words.

Resources