1

I'm trying to upload a file to a salesforce sandbox from my machine using python and simple_salesforce. The file is uploaded to salesforce but is uploaded empty. Can anyone tell me why and how do I fix it? Thanks

Python Script

import base64
import json
from simple_salesforce import Salesforce

userName = 'username'
passWord = 'password'
securitytoken = 'securitytoken'

sf=Salesforce(username='userName', password='passWord', security_token='securitytoken', sandbox = True)
sessionId = sf.session_id

body = ""
with open("Info.txt", "r") as f:
body = base64.b64encode(f.read())

response = requests.post('https://cs17.my.salesforce.com/services/data/v23.0/sobjects/Document/',
headers = { 'Content-type': 'application/json', 'Authorization':'Bearer %s' % sessionId},
data = json.dumps({
    'Description':'Information',
    'Keywords':'Information',
    'FolderId': '00lg0000000MQykAAG',
    'Name': 'Info',
    'Type':'txt'
    })
)

print response.text
3

There is missed the "'Body' : body " parameter in json.dumps object. the 'body' value in the dictionary should be the base64 encoded contents of the file which is missed. you need to read and encode the file contents yourself.

It should be like this:

data = json.dumps({
    'Description':'Information',
    'Keywords':'Information',
    'FolderId': '00lg0000000MQykAAG',
    'Name': 'Info',
    'Type':'txt',
    'body':body
    })
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.