Using cURL to Test Salesforce REST APIs

Testing Salesforce APIs using cURL

What is a cURL

cURL stands for Client URL, is a command line tool and library for transferring data with URLs. cURL supports HTTPS and performs SSL certificate verification by default when a secure protocol is specified such as HTTPS. To know more about curl.
cURL is pre-installed on many Linux and Mac systems. Windows users can download curl here.
 
curl

First, we need to collect the Base URL of your org and the Session id of the user.  

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 

Run the following piece of code in the Anonymous window.

[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: 

curl-sf-version

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:

curl-sf-account-info

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:

curl-sf-soql-query

COMMENTS

Name

Apex,6,Batch Apex,2,Chrome Add-on,1,Chrome browser,1,Coding Best Practices,2,CRM Basics,1,cURL,1,Database Query,1,Dataset,1,Dynamic Apex,3,Dynamic SOQL,1,Einstein Analytics,9,Future Apex,1,Git,1,Google Chrome,1,JS for LWC,6,Lightning Components,2,lightning Page,1,Lightning Web Components,13,Linux for Windows users,1,LWC,2,Queueable Apex,1,Redirect page,1,REGEX,1,REST API,1,Salesforce CRM,1,Salesforce Errors,1,Salesforce Interview Questions,13,Salesforce Lightning,1,Salesforce New Features,1,Salesforce Tasks,2,Schedule Apex,1,Test Class,1,Triggers,1,Visualforce Pages,3,Visualforce Pagination,2,VS Code,2,Wave Analytics,7,Winter Release Notes,1,workbench,1,
ltr
item
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog: Using cURL to Test Salesforce REST APIs
Using cURL to Test Salesforce REST APIs
Using curl to Test Salesforce REST APIs,Testing Salesforce APIs using cURL,Execute a SOQL Query Using Curl, setup curl for salesforce users,
https://1.bp.blogspot.com/-Y0QL4DoLdJc/YS42fUseO0I/AAAAAAAAAjE/qMcMBrH2F98O5D5lxjrIN9rSMxIVeDFagCLcBGAsYHQ/w640-h288/curl.png
https://1.bp.blogspot.com/-Y0QL4DoLdJc/YS42fUseO0I/AAAAAAAAAjE/qMcMBrH2F98O5D5lxjrIN9rSMxIVeDFagCLcBGAsYHQ/s72-w640-c-h288/curl.png
SWDC WORLD (Software Development Center Of The World) - is a Multi author and Multi Technology Blog
https://swdcworld.blogspot.com/2021/08/using-curl-to-test-salesforce-rest-apis.html
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/
https://swdcworld.blogspot.com/2021/08/using-curl-to-test-salesforce-rest-apis.html
true
5370056087523941001
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy