Testing Salesforce APIs using cURL
What is a cURL
To get the base URL of the Salesforce org
Run the following piece of code in the Anonymous window. To know more about IDs and URL Tricks.
[system.debug('Base Url is: '+system.Url.getSalesforceBaseUrl());]
for my org, the base URL is: https://swdcworld-dev-ed.my.salesforce.com
To get the session id of the current user
[system.debug('Session Id is: '+system.UserInfo.getSessionId().subString(15));]
Session id for a user looks like this: !ARcAQK3kpFqq2_Dp34t3S2YqFLL1WoOI5u.ijjIoZrNiz0TNFdst2LPZ8Y64dffa3DYr5yi5zbd1PKzggPOTzfqwerGtRpS4
Now open the Windows PowerShell program.
Get the salesforce version Using curl
Now let's test a simple request to get the salesforce version, for this, we don't need a session id.
[curl https://baseUrl/services/data/]
Here change baseUrl with your org base URL.
[curl https://swdcworld-dev-ed.my.salesforce.com/services/data/]
Response:
Get Basic Account object Information Using curl:
[curl https://baseUrl/services/data/v52.0/sobjects/Account/ -H "Authorization: Bearer access_token" -H "X-PrettyPrint:1"]
Here Replace baseUrl with your base url
Replace access_token wth your access token.
X-PrettyPrint: Is for response to show in easy to readable format.
-H : Means Request Header, for list of supported options for curl.
Here is Actual Request:
[curl https://swdcworld-dev-ed.my.salesforce.com/services/data/v52.0/sobjects/Account/ -H 'Authorization: Bearer !ARcAQK3kpFqq2_Dp34t3S2YqFLL1WoOI5u.ijjIoZrNiz0TNFdst2LPZ8Y64dffa3DYr5yi5zbd1PKzggPOTzfqVnxdlKpS4' -H 'X-PrettyPrint:1']
Response:
Execute a SOQL Query Using Curl
[curl https://baseUrl/services/data/v52.0/query?q=SELECT+name+from+Account+Limit+10 -H "Authorization: Bearer access_token" -H "X-PrettyPrint:1"]
Sample Request:
[curl https://swdcworld-dev-ed.my.salesforce.com/services/data/v52.0/query?q=SELECT+name+from+Account+Limit+10 -H 'Authorization: Bearer !ARcAQK3kpFqq2_Dp34t3S2YqFLL1WoOI5u.ijjIoZrNiz0TNFdst2LPZ8Y64dffa3DYr5yi5zbd1PKzggPOTzfqVnxdlKpS4' -H 'X-PrettyPrint:1']
Response:
COMMENTS