Take the 2-minute tour ×

I'm trying my hand at making a rest full ruby service. The way my program is broken down is 2 applications. One is a web service spider and the other is a web app (yet to be developed) The web app will make requests to the crawler using get post and the usual stuff.

Heres a sample post request using curl\

curl -d 'url=www.whatever.com&depth=10'

works fine and seems to post a request.

heres the delete

curl -x DELETE      where 1 is id of request.

My question is how do i make it so that If someone does a get reqeust (namely my web app) I can get the spider to respond in JSON

I'm assuming I need to format the @request variable to JSON and then have it be the response but I haven't the foggiest how to go about this.

Also on a side note: is the curl get request modeled in the same way.

On another side note: is there any way to tag code in stack overflow instead of using the four spaces.

share|improve this question
You can highlight your code and then click the "{}" code button and it will indent the 4 spaces for you. –  Jesse Wolgamott Jul 6 '11 at 17:16
add comment

3 Answers

up vote 0 down vote accepted

If you always want to return JSON no matter where the request comes from (browser or spider), then simply return json data from your controller:

render :json => @object

If you want to support a spider (JSON) and a traditional web request from a browser (HTML), use the normal respond_to (using Rails 2 example..I don't know Rails 3 yet):

respond_to do |format|
  format.js { render :json => @object }

And make sure you have the view files in the right place for the html.

share|improve this answer
format.json { render :json => @object } You can optionally use :only or :except to limit the response. –  Caley Woods Jul 6 '11 at 20:33
add comment

You can send your params in Json like this :

curl -d "{ url: \"www.whatever.com\", depth: \"10\"}" -H "Content-Type: application/json" -H "Accept: application/json"

For the response you must define your controller to response in Json format with respond_to or respond_with in Rails 3

share|improve this answer
in addition to the respond_to and respond_with ... you can force json as the response: "render :json=>@models" –  Jesse Wolgamott Jul 6 '11 at 17:17
I didnt think there were paramaters for get. I though you would just specifiy the id after /requests/2 or /3 or whatever –  overtone Jul 6 '11 at 17:19
@overtone i don't understand ! If you simply call an resource in json you make this : curl-H "Content-Type: application/json" -H "Accept: application/json" –  Joel AZEMAR Jul 6 '11 at 17:41
@Joel AZEMAR. you dont understand what? If you mean my last comment. Then what is I didnt think it was up to the client or user to specifiy what they wanted in the get request asides from the url. so basically they say GET www.whatever.com/something the page something would have to specify the type of response. But what you've told me here is that they specify the return type. e.g "content-type: application-json" I wa not aware you could do this is tyhe only point i was trying to make. –  overtone Jul 7 '11 at 8:24
add comment

Well, the response is typically output in plain HTML.

So, what you do is write a view that simply takes the response data, and formats into JSON.

The JSON format is very simple. So, if my GET response was a list of two names, for example, name = 'joe' and name = 'bob'

The JSON would be: `{ "name":"joe","name":"bob" }

... and the view code would look something like (pseudocode):

<% @names.each do |name| %>
  "name":"<%=h name %>",
<% end %>
share|improve this answer
don't do that... there is Object#to_json. Anyway, an object with repeated keys? –  tokland Jul 6 '11 at 17:53
Oh, yeah I completely forgot about that. –  jefflunt Jul 6 '11 at 18:19
add comment

Your Answer


