Update collection and singleton via api

Hello people

Well cockpit is great. I realy like the simplicity. I tried some things out but could find any answer to it.

According to the docs can create/update collection entries from frontend.

.fetch('/api/collections/save/posts?token=xxtokenxx', {
  method: 'post',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    data: {...}
  })
})
.then(res=>res.json())
.then(entry => console.log(entry));

Well I was able to create new entries but couldnt find out how do UPDATE existing entries. The code above only creates new ones. Does anyone have a clue how to do that? And is it even possible to update singletons from frontend?

Ah, and is there a way to contribute to the documentation? Like help them creating better updated docs?

thanks.

The only difference in usage between update and insert is the fact that you pass an ID along or not.

If you post with the “_id” field set in your data, cockpit will update the data determined by the id you gave; if you post without the “_id” field cockpit will create a fresh new entry.


If you’d like to contribute to the documentation, create a PR here: https://github.com/agentejo/cockpit-docs

Thank you very much for your reply! Will try it as soon I can. Is there a way to update singletons as well from frontend? I do not see like /singletons/save in the api.

And thanks for the link with the git for the docs.

the api method to call is “update_data”:

you call it this way: http://your-domain.com/singletons/update_data/my_pretty_cool_singleton_name

If you want to save singleton data via api, just create a custom api endpoint in
config/api/singletons/save/singletonname.php with this content:

<?php

return $this->invoke('Singletons\\Controller\\Admin', 'update_data', ['singletonname']);

Now send a request:
https://url.to/cockpit/api/singletons/save/singletonname?token=xxtokenxx

json post body:
{"data":{"title":"test","content":"updated content"}}

Caution: It updates empty values, too. So you always have to send all fields or the other fields will be deleted.


edit: fixed wrong folder name of custom endpoint - Thanks @SimonHayden for the correction

There is an error in @raffaelj’s, but otherwise is correct.

The php file singletonname.php should be in config/api/singletons/save/singletonname.php, which would then create an endpoint to save to Singleton “singletonname” at https://url.to/cockpit/api/singletons/save/singletonname?token=xxtokenxx

The API endpoint won’t work when not in api folder.

Equally, one could also put singletonname.php in config/api/singletonname.php and then POST data to https://url.to/cockpit/api/singletonname?token=xxtokenxx.

Hello there. I want to save singleton data via API, using only the API key without user credentials, but I can’t quite get this to work. When I POST a JSON body to my singleton save endpoint /api/singletons/save/prepared_spells?token=xyz (created as described in raffaelj’s answer) I get a response with code 302 Found but no body:

HTTP/1.1 302 Found
Date: Fri, 13 Nov 2020 00:02:00 GMT
Server: Apache/2.4.41 (Ubuntu)
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding, Cockpit-Token
Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE
Access-Control-Expose-Headers: true
Location: /auth/login?to=/api/singletons/save/prepared_spells
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

The data in the singleton isn’t updated.

The header response Location: /auth/login?to=/api/singletons/save/prepared_spells makes me believe it wants a user auth, but I would like this particular singleton to be updateable without user credentials, with only an API token.

Any pointers on what I need to adjust to get this to work would be appreciated.

Hmm I tried this but I’m getting this error,

localhost:9001/api/singletons/save/CompanyAssessment
{"error":"Call to a member function isResourceEditableByCurrentUser() on
null","file":"\/var\/www\/html\/modules\/Singletons\/Controller\/Admin.php","line":157}

Any ideas why it isn’t working?

My simple solution with revoking the method from the Admin controller doesn’t work anymore since March 2019, when the resource lock was implemented.

You have to adapt the code from modules/Singletons/Controller/Admin.phpupdate_data() in your custom endpoint to avoid the user status check.

Also trying to to this - what needs to be done to get this to work?

Thanks