简体   繁体   中英

JSON output in Ruby on Rails upon curl request

I have built a Ruby on rails app which uses HTML as default output.

Now I want to support JSON. When I use the command curl with flag --header "Accept: application/json" a JSON output should be returned.

Which implementation method do I need here? The used method may not violate the REST rules.

You need to read documentation on how to render json .

Basically, your need to tell your controller method it is to be able to respond to json :

class UsersController < ApplicationController
  def index
    @users = User.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users}
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM