function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
selrassyselrassy 

Document upload via REST API

Hello,

 

I'm trying to upload a pdf using REST API into salesforce.

 

Here's the curl:

 

https://na1.salesforce.com/services/data/v20.0/sobjects/Document/

 I'm doing a POST with a multipart/form-data. I'm sending the variable Body in the content containing my file.

 

Here's the error I'm getting:

 

[{"message":"Multipart message must include a non-binary part","errorCode":"INVALID_MULTIPART_REQUEST"}]

 

Any help?

 

Best Regards,

 

Sabine

sfdcsushilsfdcsushil

Hi,

 

I have not used REST API before. Just trying to understand. 

It seems body must consist of two parts, one is non-binary part(which contains information about the binary, e.g name , size etc.) and binary part(actual document body).

Hope this helps. 

selrassyselrassy
I saw that on the documentation and now it works fine with a .txt file but
not With a .pdf or a .doc. Ill try tout figure it out. Thanks for the help.
Best Regards,
Manoj ThangavelManoj Thangavel
Hi Team, i am also trying the same form Postman, getting the smae Error.

If you have succesfully inserted pdf document into Salesforce via REST api, let me know what is missed
Manoj ThangavelManoj Thangavel
For PDF fiel not working
User-added image
User-added image
SKrishna RSKrishna R
Hi,

Anyone have a resolution for this? Appreciate the answer!
Manoj ThangavelManoj Thangavel
Hi, Please use the below options to add an attachment via REST API. The standard multipart message not worked for me. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_code_sample_restrequest.htm Simply post with binary document and additional details(name, description and etc) can be sent via headers. Thanks, Manoj T +91 9566582294
SKrishna RSKrishna R
Hey Manoy,

 Thank you for the reply.

Understand the attachement via REST API part.

However we are looking for Content Document Creation via REST API since the file size limit for attachments is not sufficient for our implementation.

Best Regards,

Satya 
Manoj ThangavelManoj Thangavel
Yes, it is Apex code, we remove the attachment code and add the Content Version. Thanks, Manoj T +91 9566582294
Parbati Bose 6Parbati Bose 6
Hi Manoj
 is there any limitation for Content Version ? Also what would be the size limit for the blob in REST request ? can it be multipart/formdata ?
Thanks
Parbati
Manoj ThangavelManoj Thangavel
Hi Parbati, I have fixed this issue by writing an apex rest class that can receive the request body and convert that to Blob and store it as Document. It was so simple code. This solves my problem, instead of using the standard SF API for document object, we have written custom code to solve the problem. Here sample code for JPEG, similarly we have done it for pdf. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_rest_code_sample_restrequest.htm Thanks, Manoj T +91 9566582294
Cade BullCade Bull
After many hours gf searching, I have a solution in Python

I have only tested it on ContentVersion Objects, but it should also work for Document Objects
The following Salesforce link provides some details related to the contents of the POST Request
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm

In there it describes how you must send both a Binary and non-Binary Content-Type, which isn't easy (the proposed solution is to manually build your request)

Below is my code snippet for ContentVersion Object
request_headers = {'Authorization': 'Bearer ' + request_token}
    request_url = request_instance + url_object + 'ContentVersion'

    request_data = {
        'Title' : title,
        'PathOnClient' : filename,
        'ContentLocation' : location,
    }

    request_files = {'VersionData': ('2011Q1MktgBrochure.pdf', open(data, 'rb'), 'multipart/form-data'),
                     'entity_content': (None, str(request_data).replace('\'', '"'), 'application/json')}

    response = requests.request(method="POST", url=request_url, headers=request_headers, files=request_files)

Whilst I haven't tested it, below is what the code should look like for Documents (using the example JSON data from the link at the top of my response)
request_headers = {'Authorization': 'Bearer ' + request_token}
    request_url = request_instance + url_object + 'Document'

    request_data = {
        'Description': 'Marketing brochure for Q1 2011',
        'Keywords': 'marketing,sales,update',
        'FolderId': '005D0000001GiU7',
        'Name': 'Marketing Brochure Q1',
        'Type': 'pdf',
    }

    request_files = {'Body': ('2011Q1MktgBrochure.pdf', open(filename, 'rb'), 'multipart/form-data'),
                     'entity_document': (None, str(request_data).replace('\'', '"'), 'application/json')}

    response = requests.request(method="POST", url=request_url, headers=request_headers, files=request_files)

Hope this helps.
 
Shivani Vyas 1Shivani Vyas 1
Can anyone suggest how to write C# Rest api for upload attachment such as pdf and images in salesforce. I tried several ways but none of it worked for me.
Thierry CARPENTIERThierry CARPENTIER
Il faut renseigner le content type et ne pas le laisser en auto
capture ecran Postman
ds la colonne <content type> mettre la valeur <application/json>


ce qui donne au final 
curl --location --request POST 'https://xxxx.sandbox.my.salesforce.com/services/data/v56.0/sobjects/Attachment' \
--header 'Authorization: Bearer 00D5t0000008fmr!ARsAQBSiaabs4h.NMHAgeEN1lhp8V8AkcXfSC3b3AZYzJAvVivghdGpvw2HPHtsdKjv31.nwlmM3siRf6krfNs.VGYDSWxOh' \
--header 'Cookie: BrowserId=eAgSZCkKEe2adtnzEryA-Q; CookieConsentPolicy=0:1; LSKey-c$CookieConsentPolicy=0:1' \
--form 'body=@"22-05.csv"' \
--form 'contenu="{
\"ParentId\":\"5005t000004Bxe1AAC\", 
\"Name\" : \"test_TCA2.csv\"
}";type=application/json'