【问题标题】:How to create a new content entry via Locomotive CMS RESTful API如何通过 Locomotive CMS RESTful API 创建新的内容条目
【发布时间】:2014-10-10 17:58:19
【问题描述】:

我使用 LocomotiveCMS 创建了一个网站,我创建了两种内容类型,称为照片和画廊,这些内容类型之间存在关系,因此我可以在我的网站上创建图片画廊。

我目前正在寻找使用 RESTful API 以便在 Photo 遍历文件时为其创建多个内容条目。

我可以毫无问题地连接到 API 并修改站点等。

我假设新内容条目的 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'

但是我不确定如何在这个命令中传递一个文件,我现在已经用这个代替了,你会怎么写这部分?

我的照片字段设置如下:

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-on-rails locomotivecms


    【解决方案1】:

    我最终制作了一个 Ruby 脚本来解析文件并通过发送帖子数据来上传它们

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

    【讨论】:

      【解决方案2】:

      以下代码可能有助于完成此任务:

      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,因为我们实际上需要进行多部分发布。有关如何使用 curl 执行此操作的有用信息: https://github.com/locomotivecms/documentation/pull/175

      要获得令牌,您需要这样的东西:

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

      希望对你有帮助。

      【讨论】:

        【解决方案3】:

        现在有一个 LocomotiveCMS 的 api gem,适用于 2.5.x 和 3.x https://github.com/locomotivecms/coal

        对于type=file https://github.com/locomotivecms/engine/pull/511/commits/f3a47ba5672b7a560e5edbef93cc9a4421192f0a的内容输入字段,使用的属性需要以_url结尾

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-08
          • 2018-06-15
          相关资源
          最近更新 更多