How to add file to the entity using REST


These instructions explain how you can add a specific file to the wanted entity using REST. There are a few key components needed for a successful process. The user must determine which module [module] they want to add the file to and to which entity [id] the file is attached. Authorization is done by bearer [token] and [crm_address] is your dedicated domain name.

The example below shows how you add file to the entity using HTTP request method POST:

POST  /api/v1/[module]/[id]/files HTTP/1.1
Accept: application/vnd.api+json
Content-Type: multipart/form-data; boundary=---011000010111000001101001
Authorization:  Bearer [token]
Host: [crm_address]
Content-Length:  377 

Content-Disposition: form-data; name="file"; filename="example.txt" 
Content-Type: text/plain 

Content-Disposition: form-data; name="attributes[file_usage_type]" 

Entity Attachment 
Content-Disposition: form-data; name="attributes[file_category]" 

Content-Disposition: form-data; name="attributes[file_access_type]" 


The example above is similar to a standard HTML form with file upload. Which local file to use is specified in the above call with name=”file”; filename=”example.txt” so this example will add file example.txt to the wanted entity. File attributes in the example are the ones that are necessary, but you can add other attributes to them as well.

Next example shows how you make POST request using Curl:

curl --request POST \
  --url https://[crm_address]/api/v1/[module]/[id]/files \
  --header 'Accept: application/vnd.api+json' \
  --header 'Content-Type: multipart/form-data' \
  --header 'Authorization: Bearer [token]' \
  --form 'file=@[full_file_path]' \
  --form 'attributes[file_usage_type]=Internal' \
  --form 'attributes[file_category]=Category'

In the example above, which local file is used is defined in [full_file_path] where the absolute path of the desired file is entered.

2022-02-10, February Release Notes
CRM-service CIWS Library for REST
How to add file to the entity using REST
Combined Shape