sab
sab

Reputation: 97

Salesforce Bulk Upsert with Python Requests library returns ClientInputError : LineEnding is invalid on user data

I am unsuccessfully attempting to upsert csv data to Salesforce via the Python Requests library. I am getting an error reported in the Salesforce interface, stating that "ClientInputError : LineEnding is invalid on user data. Current LineEnding setting is LF".

When I request the result, I am getting a <Response [200]> (Request successfully processed).

I have tried the following:

Any ideas?

Here's my code:

    # Generate token
    params = {
        'grant_type':'password',
        'username': USERNAME,
        'password': SALESFORCE_PASSWORD,
        'client_id': SALESFORCE_CLIENT_ID,
        'client_secret': SALESFORCE_CLIENT_SECRET
    }

    r = requests.post('https://SALESFORCEWEBADDRESS/services/oauth2/token', params=params)

    access_token = r.json().get("access_token")
    instance_url = r.json().get("instance_url")
    print("Access Token:", access_token)
    print("Instance URL", instance_url)

    # STEP 3 OF UPLOADING TO SALESFORCE: Create a job

    headers = {
        'Authorization': 'Bearer '+ access_token,
        'Content-Type': 'application/json; charset=UTF-8',
        'lineEnding' : 'CRLF',
        'Accept': 'application/json'
    }

    url = 'https://SALESFORCEWEBADDRES/services/data/v53.0/jobs/ingest/' 

    request_body = {
        "object" : "Account",
        "externalIdFieldName": "Client",
        "contentType" : "CSV",
        "operation" : "upsert"
    }


    create = requests.post(headers=headers, data=json.dumps(request_body),url=url)
    print(create.text)

    # STEP 4 OF UPLOADING TO SALESFORCE: Upload csv
    headers = {
        'Authorization': 'Bearer '+ access_token,
        'Content-Type': 'text/csv',
        'Accept': 'application/json',  # I blanked this out to test and got a 409
        'lineEnding' : 'CRLF'
    }

    url = f'https://SALESFORCEWEBADDRESS/{step_2_url}'

    #data = {'file' :open('salesforce_single_records.csv', 'rb')}
    csv = 'salesforce_single_records_test.csv'
    # data = open('salesforce_single_records.csv', 'rb').read()

    with open(csv, 'rb') as f:
        upload = requests.put(headers=headers, url=url, files={csv: f})
        print(upload)
        
    # STEP 5 OF UPLOADING TO SALESFORCE: Close the job
    headers = {
        'Authorization': 'Bearer '+ access_token,
        'Content-Type': 'application/json; charset=UTF-8',
        'Accept': 'application/json'
    }

    url = f'https://SALESFORCEWEBADDRESS/services/data/{version}/jobs/ingest/{id}/' 


    request_body = {
        "state" : "UploadComplete"
    }


    close = requests.patch(headers=headers, data=json.dumps(request_body),url=url)

    print(close.text)

Upvotes: 3

Views: 1273

Answers (1)

sab
sab

Reputation: 97

And after 5 days I found the answer!

  1. In the first request body, add the line 'lineEnding' : 'CRLF':
request_body = {
    "object" : "Account",
    "externalIdFieldName": "Client",
    "contentType" : "CSV",
    'lineEnding' : 'CRLF',
    "operation" : "upsert"
}
  1. STEP 4 OF UPLOADING TO SALESFORCE: Upload csv, data needs to = payload:

csv = yourcsv

with open(csv, 'rb') as payload:
    upload = requests.put(headers=headers, url=url, data=payload)

Upvotes: 3

Related Questions