2014-10-10 31 views
0

我創建了一個使用LocomotiveCMS的網站,我創建了兩種稱爲照片和圖庫的內容類型,這些內容類型有一個關係,以便我可以在我的網站上創建圖像庫。如何通過機車CMS創建新的內容條目CMS RESTful API

我目前正在尋找使用RESTful API來爲照片創建多個內容條目,因爲它穿過一個文件。

我可以連接到API,沒有問題,修改網站等

我會假設,一個新的內容條目捲曲命令將採取以下形式:

curl -X POST -d 'photo[image_id]=blah&photo[gallery]=1234&photo[file]=<filepath>photo[published]=true' 'http://<your site>/locomotive/api/current_site.json?auth_token=xxxx' 

不過我我不確定如何通過這個命令傳遞一個文件,我現在已經替換了這個文件,你會如何編寫這個部分?

我字段設置爲圖片如下:

fields: 

- image_id: 
label: Image ID 
type: string 
required: true 
localized: false 

- file: # Name of the field 
label: File 
type: file 
required: true 
localized: false 

- gallery: # Name of the field 
label: Gallery 
type: belongs_to 
required: true 
localized: false 
# Slug of the target content type (eg post if this content type is a comment) 
class_name: gallery 

回答

0

我最終作出一個Ruby腳本解析文件,並將其上傳通過後的數據發送到

/locomotive/api/content_types/photos/entries.json?auth_token=XXXX 
0

下面的代碼可能有助於此任務:

data = { 
    content_entry: { 
     title: 'Title', 
     image: File.new('media/images/screen.png'), 
    } 
} 

HTTMultiParty.post(
    "http://localhost:8080/locomotive/content_types/blogs/entries.json?auth_token=#{@token}", 
    query: data, 
    headers: { 'Content-Type' => 'application/json' } 
) 

我使用HTTMultiParty,因爲我們實際上需要做一個多部分職位。如何與捲曲做到這一點有用的信息: https://github.com/locomotivecms/documentation/pull/175

獲得令牌,你需要這樣的事:

HTTParty.post(
    'http://localhost:8080/locomotive/api/tokens.json', 
    body: { api_key: 'YOUR_API_KEY_HERE' } 
) 

我希望幫助。