Making calls to the Oracle Cloud API using Postman

Photo credit: Photo by Adis Bacinovic from Pexels
Often when I’m building an automated solution, I’ll look at the documentation for the OCI Terraform Provider. Terraform is my typical go-to tool these days for automating OCI infrastructure, so it’s a logical place to start. Thankfully the OCI Terraform Provider does a good job of sticking pretty close to the OCI API (the terminology it uses, etc.). Sometimes this just doesn’t cut it, though, and I have to dig a level deeper. This is where I typically dive into the OCI API docs.
The OCI API
OCI has a terrific API. In case you’ve not had the pleasure of perusing it, check it out at docs.oracle.com/en-us/iaas/api. It’s worth digging in to.
To interact with the OCI API, there’s an somewhat involved signing process, which means that you can’t just easily “curl” it with off-the-shelf curl. There are plenty of exmaples, though, making it easier than ever to interact with the OCI API with your favorite language. I’ve shared a quick-and-dirty way to interact with the OCI API using Ruby in another article which gives a fast way to access the OCI API with Ruby. The kicker is that this isn’t very easy for me to really explore the OCI API.
Interacting with the OCI API
This is where tools like Paw and Postman shine. They’re built to make it really easy to interact with APIs. What I need to have handy is a tool that allows me to easily (and quickly) interact with the OCI API. I opted to go with Postman, as it doesn’t require a purchase (at least for the bare-bones functionality).
I started searching for some different options around how to get Postman to send the necessary format and stumbled across this great article which covered exactly what I was looking for. I followed the directions, essentially doing the following:
First try
- Cloning the repo
- Importing the
OCI_Environment
environment - Setting the values for the
tenancyId
,authUserId
,keyFingerprint
andprivateKey
variables (only setting theCurrent Value
, not theInitial Value
for each). - Importing the
OCI_REST_INITIALIZATION
collection - Running the
ONE_TIME_INITIALIZATION_CALL
call within theOCI_REST_INITIALIZATION
collection - Importing the
OCI_REST_COLLECTION
- Modifying the
GET_OCI_ANNOUNCEMENTS
call for the proper endpoint andcompartmentId
At this point I tried to run GET_OCI_ANNOUNCEMENTS
, but didn’t have any success. I validated all of my credentials and they were correct… What?! Why?!
I noticed that my API key had a password, but I hadn’t entered it anywhere. This is when I face-palmed myself, realizing that I’d failed to set the passphrase
variable (again, just the Current value
). After doing that, it worked great!
Ugh. Some of the simplest things can present the greatest challenges!
Finally working
To summarize, here are the steps I took that worked for me:
- Clone the repo
- Import the
OCI_Environment
environment - Set the values for the
tenancyId
,authUserId
,keyFingerprint
,privateKey
andpassphrase
variables (only setting theCurrent Value
, not theInitial Value
for each). - Import the
OCI_REST_INITIALIZATION
collection - Run the
ONE_TIME_INITIALIZATION_CALL
call within theOCI_REST_INITIALIZATION
collection - Import the
OCI_REST_COLLECTION
- Modify the
GET_OCI_ANNOUNCEMENTS
call for the proper endpoint andcompartmentId
With that out of the way, I moved forward. I was thrilled by the ability to easily make calls, but I needed the ability to move between different OCI environments easily.
Multiple Postman Environments
Postman Environments are designed to allow you to quickly switch as needed. I wanted to leverage a different Postman Environment for each OCI tenancy I needed to access, while using this handy-dandy script. I tried duplicating the OCI_Environment
in the Postman Environment, making sure that the values got moved with it, but that didn’t seem to work. Eventually I found that I was copying the private key contents from one Environment to another, and that the formatting was being stripped/changed (within Postman). The solution was to copy the key contents and paste a fresh copy into the new Environment. Voila, it worked!
Refactoring the script
Now, I should have submitted a PR, but I’m writing it here so I can share it for now: to get this super awesome script to work with variables, there are a few small modifications that could be made:
Original line | New line |
---|---|
var host = pm.request.url.host.join(".") ; |
var host = pm.variables.replaceIn(pm.request.url.host.join(".")); |
var escapedTarget = encodeURI(request.url.split(pm.request.url.host.join("."))[1]); |
var escapedTarget = encodeURI(pm.variables.replaceIn(request.url.split(pm.request.url.host.join(".")))[1]); |
body = pm.request.body.raw; |
body = pm.variables.replaceIn(pm.request.body.raw); |
The above few changes seem to allow me to use variables, which allows me to greatly extend it.
Refactored call
Let’s refactor the example given in the code, changing the GET_OCI_ANNOUNCEMENTS
call. Start by adding the following variables:
Variable Lives In (Type) | Variable Name | Variable Value |
OCI_REST_COLLECTION (Collection) |
oci_domain |
oraclecloud.com |
OCI_Environment (or whatever you’re using) (Environment) |
oci_region |
<the OCI region you're talking to> |
Now go to the GET_OCI_ANNOUNCEMENTS
GET call and modify the request path to be: https://announcements../20180904/announcements?compartmentId=
. This tells it to use the OCI region specified in the Environment, as well as the tenancyId
(also specified in the Environment) and the OCI domain name that’s set in the Collection.
Conclusion
As usual, something that seemed so cut and dry ended up being quite a bit more involved. But we’ve got a solution now, and a way to interact with the OCI API using Postman on our local computer. Yay!