简体   繁体   English

如何通过机车CMS RESTful API创建新的内容条目

[英]How to create a new content entry via Locomotive CMS RESTful API

I have created a site using LocomotiveCMS, I have created two content types called Photo and Gallery, these content types have a relationship so that I can create image galleries on my site. 我使用LocomotiveCMS创建了一个网站,我创建了两个内容类型,分别称为照片和画廊,这些内容类型具有关系,以便可以在我的网站上创建图像库。

I am currently looking to use the RESTful API in order to create multiple content entries for Photo as it traverses through a file. 我目前正在寻找使用RESTful API来为Photo遍历文件创建多个内容条目。

I can connect to the API with no issue and modify the site etc. 我可以毫无问题地连接到API并修改网站等。

I would assume that the cURL command for a new content entry would take the form of: 我假设用于新内容条目的cURL命令将采用以下形式:

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'

However I am unsure how to pass a file through in this command, I have substituted this for for now, how would you write this part? 但是我不确定如何在此命令中传递文件,我暂时已将其替换,您将如何编写此部分?

My fields are set up as follows for Photo: 对于“照片”,我的字段设置如下:

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

我最终制作了一个Ruby脚本来解析文件并通过将发布数据发送到

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

The following code can potentially help with this task: 以下代码可以帮助完成此任务:

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' }
)

I'm using HTTMultiParty since we actually need to do a multipart-post. 我正在使用HTTMultiParty,因为我们实际上需要做一个多部分的帖子。 Helpful information on how to do this with curl: https://github.com/locomotivecms/documentation/pull/175 有关如何使用curl的有用信息: https : //github.com/locomotivecms/documentation/pull/175

To get the token you need something like this: 要获得令牌,您需要这样的东西:

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

I hope that helps. 希望对您有所帮助。

There is an api gem for LocomotiveCMS by now, works for 2.5.x and 3.x https://github.com/locomotivecms/coal 现在有一个LocomotiveCMS的api宝石,适用于2.5.x和3.x https://github.com/locomotivecms/coal

the attribute used need to end with _url for content entry fields with type=file https://github.com/locomotivecms/engine/pull/511/commits/f3a47ba5672b7a560e5edbef93cc9a4421192f0a 对于类型为== https://github.com/locomotivecms/engine/pull/511/commits/f3a47ba5672b7a560e5edbef93cc9a4421192f0a的内容输入字段,使用的属性必须以_url结尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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