Rest API - Create / Update Item

22 views
Skip to first unread message

K Dos

unread,
Apr 1, 2021, 6:55:20 AM4/1/21
to Sahana-Eden
I try to implement a custom front-end for Sahana Eden (see last thread) and I think I am pretty close using community's advice.

However, I am not sure that Rest API is fully capable of working as a CRUD service for an external web app. While I can READ data (e.g. GET: /eden/org/organisation.json) or DELETE (e.g. DELETE: /eden/org/organisation/4/delete), I am not able to CREATE or UPDATE.

I have tried:
POST: /eden/org/organisation
with request data: {name: 'dummy'}

POST/PUT: /eden/org/organisation/4
with request data: {name: 'dummy'}

Neither of the above makes any changes. I dont get any errors, it just fails to CREATE/UPDATE. Can it be that request data is missing properties? Anything else? Thanks!

Dominic König

unread,
Apr 1, 2021, 7:09:16 AM4/1/21
to sahan...@googlegroups.com
The API cannot guess the data format (it's a multi-format API), you will have
to specify it as file extension in the URL, e.g.
PUT /eden/org/organisation.xml...when encoding the data as S3XML
PUT /eden/org/organisation.s3json...when encoding the data in S3JSON.

These two formats are the native formats, but some other formats work as well
(e.g. CAP for alerts, or EDXL-HAVE for hospitals).

If you omit the format extension, HTML form data is assumed - not JSON.

Dominic
signature.asc

Dominic König

unread,
Apr 1, 2021, 7:19:08 AM4/1/21
to sahan...@googlegroups.com
Try this, for instance:

PUT /eden/org/organisation.s3json
with request data = '{"$_org_organisation": [{"name": "test"}]}'

...to create a new organisation record.

Dominic
> --
> You received this message because you are subscribed to the Google Groups
> "Sahana-Eden" group. To unsubscribe from this group and stop receiving
> emails from it, send an email to sahana-eden...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sahana-eden/2092219.1BCLMh4Saa%40aldebara
> n.

signature.asc

Dominic König

unread,
Apr 1, 2021, 7:40:14 AM4/1/21
to sahan...@googlegroups.com
P.S.

S3JSON is not a special format (it's just JSON), but rather a specific object
hierarchy and naming convention for the JSON data to encode complex data sets.

You can read more here:
https://eden.sahanafoundation.org/wiki/S3/S3XML#JSONFormat

The flat .json format you saw is only supported for exports, but for CRUD you
must use S3JSON (or a format that transforms into it).

Dominic
> https://groups.google.com/d/msgid/sahana-eden/1885458.8hb0ThOEGa%40aldebara
> n.

signature.asc

K Dos

unread,
Apr 1, 2021, 10:23:37 AM4/1/21
to Sahana-Eden
Once again, thank you!

One last question: how can I UPDATE an item, I still did not figure it out. I tried like
'/org/organisation/update.s3json'
'/org/organisation.s3json' (with same id in data as other existing item)

but nothing worked. in best case, a new item was added.

Dominic König

unread,
Apr 1, 2021, 11:05:36 AM4/1/21
to sahan...@googlegroups.com
Hmm,

you simply address the particular record by adding its ID to the URL, e.g.
org_organisation#15 by:

POST /org/organisation/15.s3json
data = {"$_org_organisation": [{"name": "new name"}]}

Another option is to add the UUID of the record to the JSON:

POST /org/organisation.s3json
data = {"$_org_organisation": [{"@uuid": "urn:uuid:
191a354f-57e2-4572-9bbc-6af3768e81b5", "name": "yet another name"}]}

That way, you can update multiple records at once - and at the same time this
is an "update-or-insert" method, i.e. if no record with that UUID exists, it
will be created.

Using UUIDs is the recommended way to address records across systems - because
you cannot rely on the ids (they could change if the system is restored from a
backup, for instance - but UUIDs never change).

Dominic
signature.asc

Dominic König

unread,
Apr 1, 2021, 11:13:46 AM4/1/21
to sahan...@googlegroups.com
torsdag 1 april 2021 kl. 17:05:29 CEST skrev du:
> Using UUIDs is the recommended way to address records across systems -
> because you cannot rely on the ids (they could change if the system is
> restored from a backup, for instance - but UUIDs never change).

That said, if you just extract the record ID from the server, let the user
make a change, and then immediately send the change back to the server, then
of course you can use the ID to address the record.

It's just when you replicate the record into another data repository, and send
updates through a different process and/or at a much later point in time, you
shouldn't trust the ID you originally received, but always use the UUID
instead.

For purely interactive CRUD, though, there's no need for that complication ;)

Dominic
signature.asc
Reply all
Reply to author
Forward
0 new messages