Invoke OCI REST APIs through CURL
In this blog, we will be discussing on how to invoke OCI REST APIs through CURL.
To execute this, we can run the CURL commands in both Linux & Windows.
For Windows, the commands would look almost the same and we have to modify the escape characters.
Also, the same can be achieved both on your local machine and on Virtual Machine as well. Installation details for the same are mentioned through the above hyperlinks.
Before jumping into executing CURL commands, let us see few more things like making calls to Oracle REST APIs through OCI CLI using Python which is installed either on Linux VM (OR) Local Windows via Command Prompt (OR) Local Windows as Python executable file.
Assumption is that we have Python, OCI-CLI installed and OCI directory created with necessary Private Key & config files.
Testing connectivity through Linux VM
- Let us test the Python Configuration and it’s connectivity to our OCI environment using the below:
Testing connectivity through our Local Windows via Command Prompt
- Let us test the same on our Local Desktop i.e., through Windows where we have setup OCI-CLI for Windows.
Testing connectivity through our Local Windows via Command Prompt as a Python script file
- You can also run this by saving the content in a .py file (as we are using python SDKs).
And execute the python file as below
Now, we will see on how to fetch announcements from your OCI Console via CURL command (which is the main subject of our blog).
In this blog, I will explain the process on Linux VM
The general syntax for CURL command would be as below
curl -v -X GET -sS <oci-service-url> -H “date: <date in GMT format>” -H “Authorization: Signature version=\”1\”,keyId=\”<keyId>\”,algorithm=\”rsa-sha256\”,headers=\”(request-target) date host\”,signature=\”<your_signature>\””
<oci-service-url> → You can get this from here based on the service you want to invoke.
<date in GMT format> → You need to run the command “date -u “+%a, %d %h %Y %H:%M:%S GMT”” to fetch date here.
<keyId> → This is a combination of Tenancy-OCID/User-OCID/FingerPrint i.e., ocid1.tenancy.oc1..<unique_ID>/ocid1.user.oc1..<unique_ID>/<key_fingerprint>
<your_signature> → As you see that we need 3 headers to make a CURL call i.e., (request-target), date & host. So, our signature should contain data of the same.
Let us take an example, to demonstrate on how to build the REST URL for fetching Announcements from OCI.
As per the above syntax, I would need the below:
Fetching <oci-service-url>
The above URL should be suffixed with the below to fetch the list of announcements.
<oci-service-url> → https://announcements.us-phoenix-1.oraclecloud.com/20180904/announcements
Fetching <date in GMT format>
Run the below command to fetch current date in GMT format
Fetching <keyId>
KeyId → Not mentioning this as by this time you must be aware on how to fetch Tenancy OCID, User OCID and Key Finger Print. If not, please refer here.
Generating <your signature>
Before generating the signature, verify that the private key being used is for the same fingerprint or not by running the below command.
Now, let us generate the signature as below
CURL Command
Now, let us invoke the CURL command to list Announcements from OCI using the details we have gathered above.