Skip to content

Instantly share code, notes, and snippets.

@surajp
Last active February 9, 2023 19:15
Show Gist options
  • Save surajp/30eb59aa141702811b3056158f8cde25 to your computer and use it in GitHub Desktop.
Save surajp/30eb59aa141702811b3056158f8cde25 to your computer and use it in GitHub Desktop.
A script for invoking SF REST APIs from the command line using session id from sfdx
#!/usr/bin/env bash
set -euo pipefail
# A script for invoking SF REST APIs using session id from sfdx
if [[ $1 = "-h" || $1 = "-help" ]]; then
echo "Usage: $(basename $0) <org username/alias> <path starting from version number> <additional request info>"
exit 1
fi
orgName=$1
path=${2#\/} #remove any leading slash. we'll add it ourselves
request=${@:3}
json=$(sfdx force:org:display --json -u $orgName)
sessionId=$(echo $json | jq -r '.result.accessToken')
instanceUrl=$(echo $json | jq -r '.result.instanceUrl')
curl -H "Authorization: Bearer $sessionId" -H "Content-Type: application/json" -H "Accept: application/json" $request $instanceUrl/services/data/$path
echo "" #Newline after output
@surajp
Copy link
Author

surajp commented Feb 9, 2023

the script seems to have a problem with spaces in json data. pass the data in via a file to avoid this problem. you could use a heredoc as well as shown below

./sfRestApi.sh test@example.com v56.0/sobjects/Account -X POST -d @- <<< '{"Name":"Acme Inc","Industry":"Agriculture"}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment