Uploading Attachments to Salesforce API via Beatbo

2019-02-26 16:35发布

问题:

I'm uploading documents to Salesforce using beatbox and python and the files are attaching correctly but the data contained within the files gets completely corrupted.

def Send_File():
    import beatbox
    svc = beatbox.Client()  # instantiate the object
    svc.login(login1, pw1)  # login using your sf credentials

    update_dict = {
        'type':'Attachment',
        'ParentId': accountid,
        'Name': 'untitled.txt',
        'body':'/Users/My_Files/untitled.txt',
            }
    results2 = svc.create(update_dict)
    print results2

output is:

    00Pi0000005ek6gEAAtrue

So things are coming through well, but when I go to the salesforce record 00Pi0000005ek6gEAA and view the file the contents of the file are:

   ˝KÆœ  Wøä ï‡Îä˜øHÅCj÷øaÎ0j∑ø∫{b∂Wù

I have no clue what's causing the issue and I can't find any situations where this has happened to other people

Link to SFDC Documentation on uploads

回答1:

the 'body' value in the dictionary should be the base64 encoded contents of the file, not the file name. you need to read and encode the file contents yourself. e.g.

body = ""
with open("/Users/My_Files/untitled.txt", "rb") as f:
    body = f.read().encode("base64")

update_dict = {
    'type' : 'Attachement'
    'ParentId' : accountId,
    'Name' : 'untitled.txt',
    'Body' : body }

...

Docs about Attachment