0

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.

I am currently looking to use the RESTful API in order to create multiple content entries for Photo as it traverses through a file.

I can connect to the API with no issue and modify the site etc.

I would assume that the cURL command for a new content entry would take the form of:

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
Aaron
  • 49
  • 1
  • 8

3 Answers3

0

I ended up making a Ruby Script to parse files and upload them by sending the post data to

/locomotive/api/content_types/photos/entries.json?auth_token=XXXX
Aaron
  • 49
  • 1
  • 8
0

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. Helpful information on how to do this with 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.

rryter
  • 731
  • 7
  • 13
0

There is an api gem for LocomotiveCMS by now, works for 2.5.x and 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

Severin Ulrich
  • 421
  • 4
  • 13