How-to attach files to $file fields
Files in $file fields need two separate operations to create:
- Fill the field value with the file name
- Upload the file
Attaching a file to a new instance
We will first create the instance, filling the field "Attachment" with the name of the file that we want to attach.
curl -sS -H 'Content-type: application/json' -H 'Cookie: cobtoken=<COBTOKEN>'
"https://learning.cultofbits.com/recordm/recordm/instances/integration"
-X POST -d '{"type": "Country Events", "values": {"Attachment": "file_name.txt"}}'Running the cURL we get back the created instance:
>curl -sS -H 'Content-type: application/json' -H 'Cookie: cobtoken=<COBTOKEN>'
"https://learning.cultofbits.com/recordm/recordm/instances/integration"
-X POST -d '{"type": "Country Events", "values": {"Attachment": "file_name.txt"}}'
{"id":46994,"attachmentPath":"46994","version":0,"instanceLabel":["1665487821892"],"jsonDefinition":{"id":99,"name":"Country Events","description":"@e-learning demo","duplicable":false,"state":"enabled"},"fields":[{"id":237826,"parent":null,"fieldDefinition":{"id":1312,"name":"Start","description":"$datetime $default(now) $instanceLabel","condition":null,"required":null,"duplicable":false,"groupField":false,"order":0,"fields":[]},"value":"1665487821892","duplicate":false,"off":false,"fields":[]},{"id":237827,"parent":null,"fieldDefinition":{"id":1313,"name":"Warning","description":"$[*Nothing,Fire,Weather,UV] $expanded $instanceDescription $groupEdit","condition":null,"required":"mandatory","duplicable":true,"groupField":false,"order":1,"fields":[]},"value":"Nothing","duplicate":false,"off":false,"fields":[]},{"id":237828,"parent":null,"fieldDefinition":{"id":2216,"name":"Attachment","description":"$file","condition":null,"required":null,"duplicable":false,"groupField":false,"order":5,"fields":[]},"value":"file_name.txt","duplicate":false,"off":false,"fields":[]}]}From the instance we need to extract two important pieces of information: the id of the created instance, and the field definition id of the "Attachment" field. We can see that we created the instance 46994 and that the "Attachment" field has the definition id of 2216.
With this information we can build the request to upload the file:
curl -sS -H "Content-Type: multipart/form-data" -H 'Cookie: cobtoken=<COBTOKEN>'
"https://learning.cultofbits.com/recordm/recordm/instances/78133/files/2216"
-X POST -F "file=@file_name.txt"And running it we get back an html excerpt with the final uploaded file name:
>curl -sS -H "Content-Type: multipart/form-data" -H 'Cookie: cobtoken=<COBTOKEN>'
"https://learning.cultofbits.com/recordm/recordm/instances/78133/files/2216"
-X POST -F "file=@file_name.txt"
<textarea>file_name.txt</textarea>Attaching a file to an existing instance
We first fill the field value with the file name, using the Integration update endpoint. We are targeting the instance 46992, and filling a field named "Attachment".
curl -sS -H 'Content-type: application/json' -H 'Cookie: cobtoken=<COBTOKEN>'
"https://learning.cultofbits.com/recordm/recordm/instances/integration"
-X PUT -d '{"type": "Country Events", "condition": "recordmInstanceId:46992",
"values": {"Attachment": "another_file_name.txt"}}'Then we can upload the file:
curl -sS -H "Content-Type: multipart/form-data" -H 'Cookie: cobtoken=<COBTOKEN>'
"https://learning.cultofbits.com/recordm/recordm/instances/78133/files/2216"
-X POST -F "file=@another_file_name.txt"And running it we get back an html excerpt with the final uploaded file name:
>curl -sS -H "Content-Type: multipart/form-data" -H 'Cookie: cobtoken=<COBTOKEN>'
"https://learning.cultofbits.com/recordm/recordm/instances/78133/files/2216"
-X POST -F "file=@another_file_name.txt"
<textarea>another_file_name.txt</textarea>