Running APIs in command line
You can run the APIs in the command line by adding a bearer access token to authorization header in every API call.
About this task
To generate the bearer access token, you must create an offline token.
Procedure
-
Click the User icon
, and select Create Token to obtain an offline token.
The token is automatically generated and displayed in the Create Offline User Token dialog. -
Click Copy to copy the token and save it in a secure
location.
You may assign the token to variable
OFFLINE_TOKEN
for using in the following steps. -
Generate an access token by sending a POST request to
<baseUrl>/test/rest/tokens/
with the post data below:refresh_token=$OFFLINE_TOKEN
The response JSON displays an ‘access_token’ field. -
Add HTTP header Authorization with the access token in every API call:
Authorization:
Bearer $access_token
The following example uses a bash command to start the execution:
TEST_HUB_URL=UNSET_URL PROJECT_ID=UNSET_PROJECT_ID BRANCH=UNSET_BRANCH ASSET_PATH=UNSET_ASSET_PATH if [ -n "$OFFLINE_TOKEN" ] && [[ "$PROJECT_ID" != *"UNSET"* ]] && [[ "$TEST_HUB_URL" != *"UNSET"* ]] && [[ "$ASSET_PATH" != *"UNSET"* ]]; then \ CURL_CMD="curl -k"; \ echo "Getting access token from $TEST_HUB_URL/test/rest/tokens/"; \ access_token=$($CURL_CMD -H "Accept: application/json" \ -d refresh_token=$OFFLINE_TOKEN $TEST_HUB_URL/test/rest/tokens/ | grep -oP '"access_token"\s*:\s*"\K[^"]*'); \ POST='{"testAsset": {"revision": "'$BRANCH'", "name": "'$ASSET_PATH'"},"offlineToken": "'$OFFLINE_TOKEN'"}'; \ echo "Posting execution to $TEST_HUB_URL/test/rest/projects/$PROJECT_ID/executions/"; \ execution_rsc=$($CURL_CMD -H "Content-Type: application/json" -H "Authorization: Bearer $access_token" \ -d "$POST" $TEST_HUB_URL/test/rest/projects/$PROJECT_ID/executions/); \ echo $execution_rsc; \ else \ echo "OFFLINE_TOKEN env variable is not set (UI->Account->Create Token) or PROJECT_ID/ASSET_PATH is not set"; \ fi