How to Manipulate File Attachments
Overview
An example in bash script demonstrating how to attach a file to a record using the v11 <module>/:record/file/:field
REST POST API endpoint, then retrieve it with the GET
endpoint.
Manipulating File Attachments
Authenticating
First, you will need to authenticate to the Sugar API. An example is shown below:
curl -X POST -H Cache-Control:no-cache -H "Content-Type: application/json" -d '{
"grant_type":"password",
"client_id":"sugar",
"client_secret":"",
"username":"admin",
"password":"password",
"platform":"custom_api"
}' https://{site_url}/rest/v11/oauth2/token
More information on authenticating can be found in the How to Authenticate and Log Out example and /oauth2/logout endpoint documentation.
Submitting a File Attachment
Next, we can create a Note record using the /<module
endpoint, and then submit a File to the Note record using the /<module>/:record/file/:field
endpoint.
Create Note
curl -s -X POST -H OAuth-Token:{access_token}-H "Content-Type: application/json" -H Cache-Control:no-cache -d '{
"name":"Test Note"
}' https://{site_url}/rest/v11/Notes
Add An Attachment to the Note
curl -X POST -H OAuth-Token:{access_token} -H Cache-Control:no-cache -F "filename=@/path/to/file.txt" https://{site_url}/rest/v11/Notes/7b49aebd-8734-9773-8ef1-53553fa369c7/file/filename
Response
{
"filename":{
"content-type":"text\/plain",
"content-length":13,
"name":"file.txt",
"uri":"http:\/\/<site url>\/rest\/v11\/Notes\/7b49aebd-8734-9773-8ef1-53553fa369c7\/file\/filename"
},
"record":{
"my_favorite":false,
"following":true,
"id":"7b49aebd-8734-9773-8ef1-53553fa369c7",
"name":"My Note",
"date_modified":"2014-04-21T11:53:53-04:00",
"modified_user_id":"1",
"modified_by_name":"admin",
"created_by":"1",
"created_by_name":"Administrator",
"doc_owner":"",
"user_favorites":[
],
"description":"",
"deleted":false,
"assigned_user_id":"",
"assigned_user_name":"",
"team_count":"",
"team_name":[
{
"id":1,
"name":"Global",
"name_2":"",
"primary":true
}
],
"file_mime_type":"text\/plain",
"file_url":"",
"filename":"file.txt",
"parent_type":"",
"parent_id":"",
"contact_id":"",
"portal_flag":false,
"embed_flag":false,
"parent_name":"",
"contact_name":"",
"contact_phone":"",
"contact_email":"",
"account_id":"",
"opportunity_id":"",
"acase_id":"",
"lead_id":"",
"product_id":"",
"quote_id":"",
"_acl":{
"fields":{
}
},
"_module":"Notes"
}
}
Getting a File Attachment
Next, we can retrieve the file attachment stored in Sugar by utilizing the /<module>/:record/file/:field
GET
endpoint.
curl -i -s -X GET -H OAuth-Token:{access_token} -H Cache-Control:no-cache https://{site_url}/rest/v11/Notes/7b49aebd-8734-9773-8ef1-53553fa369c7/file/filename
Response
HTTP/1.1 200 OK
Date: Wed, 12 Mar 2014 15:15:03 GMT
Server: Apache/2.2.22 (Unix) PHP/5.3.14 mod_ssl/2.2.22 OpenSSL/0.9.8o
X-Powered-By: PHP/5.3.14 ZendServer/5.0
Set-Cookie: ZDEDebuggerPresent=php,phtml,php3; path=/
Expires:
Cache-Control: max-age=0, private
Pragma:
Content-Disposition: attachment; filename="file.txt"
X-Content-Type-Options: nosniff
ETag: d41d8cd98f00b204e9800998ecf8427e
Content-Length: 16
Connection: close
Content-Type: application/octet-stream
This is the file contents.
You can also output the results directly to a file by omitting the header data and output the results directly to a new file.
curl -s -X GET -H OAuth-Token:{access_token} -H Cache-Control:no-cache https://{site_url}/rest/v11/Notes/7b49aebd-8734-9773-8ef1-53553fa369c7/file/filename > file.txt