Skip to content

Instantly share code, notes, and snippets.

@rtrouton
Last active January 5, 2022 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtrouton/4639395aeea34a193443fdb84f1f61cb to your computer and use it in GitHub Desktop.
Save rtrouton/4639395aeea34a193443fdb84f1f61cb to your computer and use it in GitHub Desktop.
Example Jamf Pro API call process
# Get username and password encoded in base64 format and stored as a variable in a script:
encodedCredentials=$(printf username_here:password_here | /usr/bin/iconv -t ISO-8859-1 | /usr/bin/base64 -i -)
# Use encoded username and password to request a token with an API call and store the output as a variable in a script:
authToken=$(/usr/bin/curl https://server.name.here/api/v1/auth/token --silent --request POST --header "Authorization: Basic ${encodedCredentials}”)
# Read the output, extract the token information and store the token information as a variable in a script:
api_token=$(/usr/bin/plutil -extract token raw -o - - <<< “$authToken”)
# Verify that the token is valid and unexpired by making a separate API call, checking the HTTP status code and storing status code information as a variable in a script:
api_authentication_check=$(/usr/bin/curl --write-out %{http_code} --silent --output /dev/null https://server.name.here/api/v1/auth --request GET --header "Authorization: Bearer ${api_token}")
#Assuming token is verified to be valid, use the token information to make an API call:
/usr/bin/curl --silent -H "Accept: application/xml" --header "Authorization: Bearer ${api_token}" https://server.name.here/JSSResource/packages/id/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment