Sub topic 11
Calling the service and debugging it
This chapter assumes you can write an @RestResource class, you know HTTP methods and status codes, and you have a Salesforce org with My Domain enabled. You will call a deployed service with an OAuth access token and learn to find the cause when a call fails.
What a call needs
Every call to an Apex REST service has three parts:
- A URL of the form
https://MyDomainName.my.salesforce.com/services/apexrest/followed by yoururlMapping. - An
Authorization: Bearer <token>header carrying an OAuth access token. - A caller whose user can run the class. That means class access through a profile or permission set, and the object and field permissions your code enforces.
The token comes from an OAuth flow, usually a connected app or external client app. The flow depends on the client. A server-to-server job often uses the client credentials flow. A command-line tool can use the Salesforce CLI.
sf org display --target-org my-org --jsonThe output includes an accessToken and the instanceUrl. This is a quick way to get a token for testing. Treat it like a password and keep it out of source control.
Making a call with curl
Store the token in an environment variable, then send a GET request. Replace MyDomainName and the record Id with your own values.
curl -H "Authorization: Bearer $ACCESS_TOKEN" \
"https://MyDomainName.my.salesforce.com/services/apexrest/v1/Accounts/001XXXXXXXXXXXXXXX"A successful call returns the Account as JSON. To create a record with the @HttpPost method, send a JSON body and a Content-Type header. Add -i to see the status line and response headers.
curl -i -X POST \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"Acme","industry":"Energy"}' \
"https://MyDomainName.my.salesforce.com/services/apexrest/v1/Accounts"The keys name and industry match the parameter names of the Apex method. A mismatch leaves the parameter null and does not raise an error.
Reading failures by status code
Most failures can be traced from the status code. Use curl -i so you can see it.
| Status | Typical cause | What to check |
|---|---|---|
| 401 | Expired, revoked or missing token | Request a new token. Confirm the host matches the org that issued it. |
| 403 | The user cannot access the class | Add the class to the user's profile or a permission set. |
| 404 | The URL does not match any `urlMapping` | Check the path, the version segment and the case. Your own handler may also return 404 for a missing record. |
| 405 | No method for this HTTP verb | Confirm the class has the matching `@HttpGet`, `@HttpPost` and so on. |
| 400 or 500 | Bad input or an unhandled exception | Read the response body, then check the debug log. |
An unhandled exception in a handler produces a 500 response with a JSON error message. Catching expected exceptions and setting res.statusCode yourself gives callers a more useful answer, as covered in the error handling chapter.
Debugging with debug logs
When the response is not enough, read the Apex debug log for the calling user.
- In Setup, open Debug Logs and add a trace flag for the user the token belongs to.
- Set the Apex Code level to
DEBUGorFINE. - Repeat the call.
- Open the new log and look for the
CODE_UNIT_STARTEDline naming your class, then follow the events beneath it.
Add temporary System.debug lines to see what the handler received.
RestRequest req = RestContext.request;
System.debug('URI: ' + req.requestURI);
System.debug('Params: ' + req.params);
System.debug('Body: ' + req.requestBody?.toString());The user in the trace flag is the user who owns the token, not you. If you trace the wrong user, no log appears.
Common mistakes
- Calling
login.salesforce.comor the wrong sandbox host instead of your My Domain URL. - Forgetting the
Content-Type: application/jsonheader on POST, PUT and PATCH. - Sending a body key that does not match the Apex parameter name.
- Expecting a deployment to be visible before it finishes. Confirm the class exists in the org first.
Exercise
A client sends a valid token and a GET request to /services/apexrest/v1/Accounts/001XXXXXXXXXXXXXXX. It receives a 403 response. Which two things do you check first?
Show answer
Check that the calling user has access to the Apex class, through a profile or permission set. Then check that the token belongs to the user you expect. A 403 means the caller was authenticated but is not allowed to run the class, so the token itself is probably valid.
Exercise
You enabled a debug log with a trace flag for your own user, but no log appears when a partner system calls the service. Why?
Show answer
The call runs as the user the partner's token belongs to, often an integration user. The trace flag must be set for that user.