REST API upload file attached to an entity

Other platform (mobile) lets say B4A or android-java, cordova or whatever, makes a request to CUBA REST-API and takes in return a JSON, lets say Routes. I write this JSON to SQLite database, property by property. Run through this table and create a related record in other table with some info, results and a Photo. This foto is stored in mobile.

Finally, the “result” entities are sent to CUBA via REST-API. How can I return each photo attached to result?
In SQLite “result” table exist string property with attached Photo name, but CUBA “result” entity uses a filedescriptor property.

You question is about how to upload an entity, one field of which is a file, right? If so, then you’ll have to do two REST API invocations. The first request will upload a file with the photo . It will be a POST request to the /files endpoint. This method will return you a JSON like this:

{
  "id": "c994111a-cdfa-9695-95fc-bbbb63a504a7",
  "name": "sample-file.docx",
  "size": 105396
}

The id field is the identifier of the FileDescriptor entity.
Your another entity, say it’s name is MyEntity, will have a field photo with the FileDescriptor type. To create an instance of the entity with the reference to the FileDescriptor of just uploaded photo you’ll need to make a request like this /entities/myproj$MyEntity:

{
   "field1": "someValue",
   "photo": {
      "id": "c994111a-cdfa-9695-95fc-bbbb63a504a7"
   }
}

Photo id here is the ID of the file descriptor returned by the first REST API request

2 Likes

Clean, thanks a lot :+1: