【问题标题】:Rails - Add/Post New Record to app through APIRails - 通过 API 添加/发布新记录到应用程序
【发布时间】:2012-06-27 02:26:34
【问题描述】:

我正在尝试向我的应用程序添加一个公共 API,以允许用户通过 API 调用将项目添加到我的应用程序中。

虽然我有点困惑......

到目前为止,我有这个输出一个很棒的 xml 页面,其中包含我们所有的模型数据非常完美....

def index   
  @events = Event.all
  respond_to do |format|
    format.html 
    format.xml  { render :xml => @events }
    format.json { render :json => @events }
  end
end

所以我假设我可以将相同的 respond_to 块添加到 CREATE 或 NEW 操作中(哪个?)并在那里获得某种形式的 API 功能???但我对整个过程的运作方式感到困惑......

例如,如果我的 Event 模型只有一个字段 => name:string

如何通过网络服务添加记录?

  ???? ==> curl http://localhost:3000/events[??????add??????]

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 api


    【解决方案1】:

    See this post 关于使用 cURL 测试 REST 应用程序。

    引用:

    -X [action]: Allows you to specify an HTTP action such as GET, POST, PUT or DELETE.
    Example:
    
    curl -X DELETE http://localhost:3000/books/1
    
    -d [parameter]: Lets you set variables as if they were POSTed in a form to the URL. Note that this automatically makes the request a POST HTTP action type (no -X necessary).
    Example:
    
    curl -d "book[title]=Test" -d "book[copyright]=1998"
    http://localhost:3000/books
    
    -H [header]: Gives you the option of setting an HTTP header such as Content-Type or Accept. This is particularly useful for requesting text/xml as the Accept type.
    Example:
    
    curl -H "Accept: text/xml"
    http://localhost:3000/books/sections/1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-29
      • 2016-08-20
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多