Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions curl_command/curl_command_examples_get_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ AUTH_URL="https://example.com/auth"
USERNAME="user"
PASSWORD="pass"

# Build the JSON payload using jq
PAYLOAD=$(jq --null-input \
--arg username "$USERNAME" \
--arg password "$PASSWORD" \
'{ "username": $username, "password": $password }')

# Use curl to authenticate and obtain a token
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" -d '{"username": "'"$USERNAME"'", "password": "'"$PASSWORD"'"}' $AUTH_URL | jq -r '.token')
TOKEN=$(curl -s -X POST -H "Content-Type: application/json" -d "$PAYLOAD" "$AUTH_URL" | jq -r '.token')

# Check if the token was obtained successfully
if [ -z "$TOKEN" ]; then
Expand All @@ -24,4 +30,4 @@ fi
RESOURCE_URL="https://example.com/resource"

# Use curl with the token to access the resource
curl -s -H "Authorization: Bearer $TOKEN" $RESOURCE_URL
curl -s -H "Authorization: Bearer $TOKEN" "$RESOURCE_URL"