Postman has been my preferred choice for quite some time when testing any MATLAB code that I am bringing to production or showcasing others how to leverage the RESTful API for MATLAB Production Server. However, I must say that I am a huge cURL fan. It is almost always available on test machines and is easy to use in scripts. Besides, I have great esteem for Daniel Stenberg, the man behind cURL.

Recently, I started to play with Thunder Client, a lightweight Rest API Client Extension for Visual Studio Code. This Extension facilitates the creation of REST API calls while staying at VS Code. Here’s a short post to share my impressions.

Install Thunder Client

Click your Extensions button at VS Code or simply click Ctrl + Shift + X. Install the extension and restart VS Code:

Thunder Client - REST API Client Extension for VS Code

Start your Production Server

One simple way, scalable and aligned with cloud development best practices, is to deploy your MATLAB code via MATLAB Production Server. MATLAB Production Server is an application server that operationalizes your MATLAB models or algorithms as APIs, so they get integrated with your enterprise IT/OT systems.

Once your algorithm has been developed and fully tested, you may package it as a CTF file using MATLAB Compiler SDK and deploy it to your production system without having to recode the algorithm in a different language or create custom infrastructure. Deployed packages can be accessed by client applications written in Java®, .NET, Python®, C and C++ using client-specific libraries or through a HTTP/HTTPS endpoint using the RESTful API.

Production Server Compiler App in MATLAB Compiler SDK facilitates the testing and packaging of algorithms. Once opened, you can add the desired files to Production Server Compiler App:

Adding files to be deployed

Test your MATLAB functions from VS Code

Production Server Compiler App simplifies testing your MATLAB functions right from your desktop before moving them to your staging or production system.

Testing Client

You can then start a local application server to test your to-be-deployed MATLAB functions:

Start local Production Server

Back in VS Code, you can now create a new request using Thunder Client:

New request Thunder Client

It’s relatively straightforward from here to create a POST request, specify the URL and configure the HTTP request payload using JSON (click image below to enlarge):

HTTP Request to Production Server from Thunder Client

The key elements in building the payload, in this case, the Body of the HTTP POST Request, are:

  • nargout: the number of outputs that the client application is requesting from the deployed MATLAB function
  • rhs: Right Hand Side of the deployed MATLAB function, that is, the input arguments to the deployed MATLAB function, specified as an array of comma-separated values
  • outputFormat: Specify whether the MATLAB output in the response should be returned using large or small JSON notation, and whether NaN and Inf should be represented as a JSON string or object

The response from the server (lhsleft hand side–) is a JSON array where each element corresponds to an output of the deployed MATLAB function.

MATLAB data types can be represented in JSON format to interchange data accross programming languages.

The example below is yet another dummy example to show how to set up the POST request and interpret MATLAB Production Server’s output.

Given the MATLAB function:

Add matrix example

we can build the following input JSON payload, requesting two outputs from the server (sum and cumulative sum).

{
    "nargout": 2, 
    "rhs": 
    [
        {
            "mwdata":[62,31,52,10,27,39],
            "mwsize": [2,3],
            "mwtype": "uint8"
        },
        {
            "mwdata":[46,18,7,3,13,11],
            "mwsize": [2,3],
            "mwtype": "uint8"
        }
    ],
    "outputFormat":
    {
        "mode":"large",
        "nanInfFormat":"string"
    }
}

We use again Thunder Client to complete the HTTP Request to MATLAB Production Server (click image below to enlarge):

HTTP Request to Production Server from Thunder Client

Note that the inputs sent to and outputs given by MATLAB should be interpreted in column-major format.

From here, you may obtain a code snippet to bring to your code or script:

Thunder Client code snippet

My take

It’s clear that Postman is indeed a complete tool with many exciting features. However, it has become more of an API development application and has lost some of its original lightweight API testing core.

If you are looking for something lightweight to do your API testing that even integrates with VS Code, Thunder Client might be an option to explore! :zap:

Comments