Sub topic 6
Uploading the data
This chapter covers the step after you create an ingest job: sending your CSV to Salesforce. It assumes you can read JSON and CSV, use HTTP methods, and already hold an OAuth access token. It also assumes you have created a job and have its id, which the chapter on creating an ingest job covers.
What the upload step does
A new ingest job starts in the Open state. In this state it accepts data but does not process anything. You send your CSV with a single PUT request to the job's batches endpoint. Salesforce stores the data with the job. Processing only starts after you close the job, which is the next chapter.
The upload request
Use PUT on /services/data/vXX.X/jobs/ingest/{jobId}/batches. Set Content-Type to text/csv. Send the file as the request body.
curl -X PUT "$INSTANCE_URL/services/data/v62.0/jobs/ingest/$JOB_ID/batches" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: text/csv" \
--data-binary @contacts.csvA successful upload returns HTTP status 201 Created with an empty body. Treat any other status as a failure and read the error message in the response.
Here is the file used above. The header row holds field API names, and each later row is one record.
FirstName,LastName,Email
Ada,Lovelace,[email protected]
Alan,Turing,[email protected]Use --data-binary, not -d
The curl option -d strips carriage returns and newlines from file contents. That corrupts a CSV, because the row breaks are gone. --data-binary sends the bytes exactly as they are in the file. Always use it for CSV uploads.
The line endings in your file must also match the lineEnding value you set when you created the job. The job in this tutorial uses LF. If you create the job with LF but upload a file with CRLF endings, Salesforce can misread the rows. Either set lineEnding to CRLF at job creation, or convert the file first.
Upload from Python
The same step in Python uses the requests library. Passing an open file object streams the file instead of loading it all into memory.
import os
import requests
instance = os.environ["SF_INSTANCE_URL"]
token = os.environ["SF_ACCESS_TOKEN"]
job_id = os.environ["SF_JOB_ID"]
url = f"{instance}/services/data/v62.0/jobs/ingest/{job_id}/batches"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "text/csv",
}
with open("contacts.csv", "rb") as f:
response = requests.put(url, headers=headers, data=f)
response.raise_for_status()
print(response.status_code)Open the file in binary mode ("rb"). Text mode can change line endings.
Rules to remember
- Upload data only while the job is
Open. Once it moves pastOpen, further uploads are rejected. - Make one
PUTrequest per job for the data. Salesforce splits the data into internal batches for you, so you do not split files yourself. - Keep the CSV within the size limit for a single upload. The limits chapter gives the current figure. If your data is larger, split it across several jobs.
- Keep column headers identical to field API names, such as
FirstName, not the display labels.
Common errors
| Symptom | Likely cause |
|---|---|
| Rows merge into one or the job reports odd values | You used `-d` instead of `--data-binary` |
| Error about the job state | The job is no longer `Open` |
| Fields not found | Header names are labels, not API names |
| Parsing problems on every row | The line endings differ from the job's `lineEnding` |
Exercise
You created a job with "lineEnding": "LF" and upload with curl -d @contacts.csv. Name two things that can go wrong and give the fix for each.
Show answer
First, -d removes newlines, so the rows are damaged. Use --data-binary @contacts.csv instead. Second, if the file has CRLF endings, they do not match the job's LF setting. Convert the file to LF, or create the job with "lineEnding": "CRLF".