Run Postman Code directly into Orchestrator [CB10125]

How easy it would if you can run Postman code snippet inside Orchestrator without creating any package or environment. Let’s see in this article how we can do it.

By

min read

Run Postman Code directly into Orchestrator [CB10125]

Introduction

This is a trick that I found with release of Orchestrator 8.11.2 which uses Node.js 18.14.2. This would be particularly helpful for those who work on Postman to test their APIs and then tries to do the same through Orchestrator.

I was casually checking what are the differences between 14 and 18 and I reached the Node.js 18’s release page where I noticed that there is a new API called fetch which is globally available and not needed to be called using any additional package which means we don’t have to care about creating any package or environment in Orchestrator while using the Node.js Runtime, which is exactly why I liked it.

There is a new API called fetch which is globally available and not needed to be called using any additional package, which inspired me to use it in Orchestrator.

Now, comes the Postman side of view.

I already knew that Postman can convert an API request into a code snippet, and you can choose the programming language or framework, one of which is the JavaScript fetch.

So, I thought what if I try to run this Postman generated code snippet into Orchestrator directly, will it work?

Guess what! It worked.

A bit on fetch API

Before that, let me quickly tell you what is fetch API? The Fetch API provides an interface to connect to the network and fetch resources form the network. Simply put, it can be used to make REST call.

It uses the Promise to deliver more flexible features to make requests to servers.

Sending a Request

The fetch() method returns a Promise so you can use the then() and catch() methods to handle it:

fetch(url)
    .then(response => {
        // handle the response
    })
    .catch(error => {
        // handle the error
    });

When the request completes, the resource is available. At this time, the promise will resolve into a Response object.

The Response object is the API wrapper for the fetched resource. This object has a number of useful properties and methods to inspect the response.

Reading the Response

If the contents of the response are in the raw text format, you can use the text() method. The text() method returns a Promise that resolves with the complete contents of the fetched resource:

fetch('/readme.txt')
    .then(response => response.text())
    .then(data => console.log(data));

In practice, you often use the async/await with the fetch() method like this:

async function fetchText() {
    let response = await fetch('/readme.txt');
    let data = await response.text();
    console.log(data);
}

Besides the text() method, the Response object has other methods such as json()blob()formData() and arrayBuffer() to handle the respective type of data.

Demo

Go to Postman and set up a request. Once you are satisfied with the request, on the left, there is a </> icon. Click there.

Select JavaScript fetch from the list. Copy this code.

Video: Generate code snippet in Postman App

Now, create a new action and select Runtime = Node.js 18. Copy the Postman generated code before callback code auto-generated by Orchestrator as shown below.

Video: Test the copied code in Orchestrator

And that is how you can quickly make any Postman REST request inside Orchestrator except the ones which requires file uploads(that would require some extra steps). I have tried other methods like POST, PUT, PATCH & DELETE. All worked smooth. Try this with the older Node.js 14 Runtime and it will not work.

Honestly, this trick brings me some peace of mind and ease of doing things while working with REST calls inside Orchestrator.

Advantages

  • First and foremost, It allows quick conversion of tested REST APIs in Postman into Orchestrator compatible code. WIN-WIN
  • Using fetch API allows hassle-free experience without dealing with adding packages (eg. requests module in Node.js), bundling zips or creating script environments etc.
  • Also, There is no need to write custom code in Orchestrator to make a REST call. It’s simple copy and paste.

If you made it to the end, I would say I am very grateful to you and hope that you find this article useful. However, to improve the credibility of the article, I would encourage you to test it in your environment in different ways and let me know in the comment if you find any discrepancies. Thank you as always. See you.

Error: SELF_SIGNED_CERT_IN_CHAIN

In case you are getting this error while executing the code in Orchestrator,

you can use this piece of code to suppress this error by putting it on top of your Orchestrator script.

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

or you can provide the path to the certificate. I haven’t tried this way yet.

process.env.NODE_EXTRA_CA_CERTS = "/path/to/your/cert.pem";

References

Leave a Reply

Related Posts

%d bloggers like this: