1

I'm using simple-salesforce to migrate attachments from one org to another. I'm encoding with base64 but am getting this error:

SalesforceMalformedRequest: Malformed request 
https://my_org.my.salesforce.com/services/data/v38.0/sobjects/Attachment/. 
Response content: [{
   'message': "Json Deserialization failed on token 'null' and has left off in the middle of parsing a row. Will go to end of row to begin parsing the next row", 
   'errorCode': 'INVALID_FIELD'}]

I've tried different attachment types but am not having any luck. Below is the code I'm using to test.

from simple_salesforce import Salesforce
from base64 import b64encode
import json

userName = 'my_username@test.com'
password = 'mypw'
securityToken = 'mytoken'
svc = Salesforce(username=userName, password=password, security_token=securityToken)
ENCODING = 'utf-8'
name = 'test.txt'
parent_id = '5001I000006rNenQAE'


with open(name, 'rb') as open_file:
    byte_content = open_file.read()
    base64_bytes = b64encode(byte_content)
    base64_string = base64_bytes.decode(ENCODING)
    json_data = json.dumps({
                    'ParentId' : parent_id,
                    'Name' : name,
                    'Body' : base64_string
                    })

    result = svc.Attachment.create(json_data)
    print(result)

Am I missing something obvious? The error Json Deserialization failed is only coming up in questions about apex not uploading via REST API.

1

simple_salesforce methods don't take JSON strings as parameters, generally - they take Python dicts directly.

svc.Attachment.create({ 'ParentId': parent_id, 'Name': name, 'Body': base64_string })

should work just fine; I've tested it out in my developer org. Presumably the extra layer of serialization is causing the null issue somewhere between your Python and Salesforce (since Python itself doesn't use the keyword null).

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.