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.

.oci directory on Linux Virtual Machine

Testing connectivity through Linux VM

  1. Let us test the Python Configuration and it’s connectivity to our OCI environment using the below:
Executing Python code on Linux VM

Testing connectivity through our Local Windows via Command Prompt

  1. Let us test the same on our Local Desktop i.e., through Windows where we have setup OCI-CLI for Windows.
Executing Python Code through Windows Command Prompt (Local)

Testing connectivity through our Local Windows via Command Prompt as a Python script file

  1. You can also run this by saving the content in a .py file (as we are using python SDKs).
Python Script file

And execute the python file as below

Executing Python Code through Windows Command Prompt as Python script file (Local)

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>

URL for all Announcement related service calls in OCI

The above URL should be suffixed with the below to fetch the list of announcements.

List Announcements API

<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

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.

Fetching Fingerprint from the Private Key file

Now, let us generate the signature as below

Request to generate Signature
Signature

CURL Command

Now, let us invoke the CURL command to list Announcements from OCI using the details we have gathered above.

CURL command to list announcements

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Santhosh Kumar BVSRK

15+ Years in IT/ITES| ML & AI Enthusiast| Integration Specialist — BPEL, OSB, OIC, IICS, Oracle Cloud Infrastructure| Database Scripting-SQL, PL/SQL, MySQL