Sub topic 5
Which HTTP method and status code goes with which operation
This chapter assumes you know basic HTTP (methods, headers, status codes) and can read JSON. It also assumes you already have an access token and know how the instance URL, API version, and sObject name combine into a resource URL. Here you learn which method and which status code belong to each record operation, so you can tell at a glance whether a call did what you intended.
The mapping
The Salesforce REST API follows standard HTTP semantics for single records. The method tells Salesforce what to do, and the status code tells you how it went.
| Operation | Method | URL | Success status | Success body |
|---|---|---|---|---|
| Create | POST | `/sobjects/Account` | 201 Created | JSON with `id`, `success`, `errors` |
| Read | GET | `/sobjects/Account/{id}` | 200 OK | The record as JSON |
| Update | PATCH | `/sobjects/Account/{id}` | 204 No Content | Empty |
| Delete | DELETE | `/sobjects/Account/{id}` | 204 No Content | Empty |
Create posts to the object name, with no ID, because the record does not exist yet. Read and update address one record, so the ID is part of the URL. Delete appears in the table for completeness, but this tutorial does not cover it.
Why PATCH and not PUT
PATCH means "change only the fields I send." You can send {"Industry": "Banking"} and every other field keeps its value. Salesforce does not use PUT to replace a record. If you are used to APIs where PUT sends the whole object, remember that here a partial body is the normal case.
What each success looks like
A create returns the new ID in the body. Keep it, because later reads and updates need it.
{
"id": "001xx000003DGb2AAG",
"success": true,
"errors": []
}An update returns no body at all. This surprises people who try to parse JSON from the response and get a decoding error. A 204 is the whole confirmation. If you want to see the new values, send a GET afterward.
You can see the status line with curl's -i flag:
curl -X PATCH "$SF_INSTANCE_URL/services/data/$SF_API_VERSION/sobjects/Account/001xx000003DGb2AAG" \
-H "Authorization: Bearer $SF_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"Industry": "Banking"}' \
-iThe first line of the output should read HTTP/1.1 204 No Content.
Checking status in code
Check the status code before you touch the body. The body's shape depends on the status: an object on 201, a record on 200, nothing on 204, and a JSON array of error objects on failures.
import requests
def describe(response: requests.Response) -> str:
if response.status_code == 201:
return f"created {response.json()['id']}"
if response.status_code == 200:
return "record returned"
if response.status_code == 204:
return "updated, no body"
return f"failed {response.status_code}: {response.text}"Common mistakes
- Parsing a 204 as JSON. The body is empty, so
response.json()raises an exception. Test the status first. - Expecting 200 from a create. A successful create returns 201. Code that compares against 200 will treat success as failure.
- Sending POST to a record URL to update it. Use PATCH. Updates go to the URL that includes the ID.
- Forgetting
Content-Type: application/jsonon POST and PATCH. The request may be rejected as an unsupported media type (status 415). - Treating any 2xx as the same. Different 2xx codes mean different body shapes, so branch on the exact code.
Exercise
You send a PATCH with {"Industry": "Banking"} to an Account record URL, and your code calls response.json() on the reply. It raises a decoding error, though the Account was updated. What happened, and how do you fix the code?
Show answer
A successful PATCH returns 204 No Content with an empty body, so there is nothing to decode as JSON. The update worked. Check that response.status_code is 204 and skip response.json(). If you need the updated values, send a GET to the same record URL.
Exercise
Which method, URL form, and success status code create an Account, and where do you find the new record's ID?
Show answer
Use POST to /services/data/{version}/sobjects/Account, with no record ID in the URL. A success returns 201 Created. The new ID is the id field in the JSON response body.