REST API to get Projects from CLI

Objective of this article to know how to call REST API from CLI to get the projects list. Continue further to know various problems and solutions to overcome and finally list the projects.

How to call REST API from CLI?
To call REST API from CLI, we can use curl -k command followed by the REST URL. For more information look at the below command,

curl -k https://192.168.99.100:8443/oapi/v1/projects

API call failed with Forbidden (403) response

How to solve Forbidden (403) issue while calling REST API?
Since we are not authorized who is calling the API, we end up with the problem of 403 Forbidden. To solve this problem, login as developer by using the following command.

oc login -u developer -p developer

user login from CLI

Then issue the below command to get the security token which we need to pass in REST API call.

oc whoami -t

Identifying the security key by issuing the command of whoami

Now pass the security key in the REST call Authorization by using -H “Authorization: Bearer PaqE4Ui1mirlpAUqi6w-20VQS0qeD3EXA8w1IhwewqE”. The complete URL looks like,

curl -k https://192.168.99.100:8443/oapi/v1/projects -H “Authorization: Bearer PaqE4Ui1mirlpAUqi6w-20VQS0qeD3EXA8w1IhwewqE”

API call success response when we don’t have any projects

Below image will show you what is the response we will get when we have already created project in OpenShift.

Response when we have already created project in OpenShift
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s