2012-06-26 53 views
0

我正在嘗試將公共API添加到我的應用程序,以允許用戶通過API調用將項目添加到我的應用程序中。Rails - 通過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功能?但我很困惑這整個過程是如何工作的?

例如,如果我的事件模型只有一個字段=>名稱:字符串

我將如何能夠通過網絡服務來添加一條記錄?

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

回答

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 
相關問題