Last active
February 9, 2023 19:15
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 [email protected] v56.0/sobjects/Account -X POST -d @- <<< '{"Name":"Acme Inc","Industry":"Agriculture"}'