Testing out REST API's

Node-RED is a useful tool for accessing remote REST API's and also for creating them.

In either case, it can be useful to be able to test the API's separate to Node-RED.

Here are my chosen tools - both use the Microsoft VSCode free code editor as the host for convenience.

  • The REST Notebook extension: Notebooks, Visual Studio Code style

    This is a fairly new extension using the new Notebook API now built into VSCode (originally developed to allow Jupyter Notebooks to work natively, a REALLY useful feature in its own right). This extension is developed by one of the core VSCode developers.

  • The REST Client extension: REST Client - Visual Studio Marketplace

    I find this really useful as you can easily create a script file for testing multiple API's very easily. As in this example for testing the API's provided by uibuilder:

// Test uibuilder v3 admin API routes
//
// Using REST Client extension https://github.com/Huachao/vscode-restclient#rest-client

// File variables
@hostname = localhost
@port = 1880
@host = {{hostname}}:{{port}}
@redAdminPath = red
@uibPath = uibuilder
@uibAdminPath = admin
@contentType = application/json
@createdAt = {{$datetime iso8601}}
# Valid uibuilder instance URL
@url = test1

# @name Sample-Get
# Get something and return it
GET http://{{host}}/{{redAdminPath}}/{{uibPath}}/{{uibAdminPath}}/{{url}}?ping=pong
content-type: {{contentType}}

{
    "name": "sample GET",
    "time": "{{createdAt}}"
}

###

# @name Sample-Put
# Write file contents
PUT http://{{host}}/{{redAdminPath}}{{uibAdminPath}}/{{url}}
content-type: {{contentType}}

{
    "name": "sample PUT",
    "time": "{{createdAt}}"
}

###

# @name Sample-Post
# Create a new folder or file
POST http://{{host}}/{{redAdminPath}}{{uibAdminPath}}/{{url}}
content-type: {{contentType}}

{
    "name": "sample DELETE",
    "time": "{{createdAt}}"
}

###

# @name Sample-Delete
# Delete a folder or a file
DELETE http://{{host}}/{{redAdminPath}}{{uibAdminPath}}/{{url}}
content-type: {{contentType}}

{
    "name": "sample DELETE",
    "time": "{{createdAt}}"
}

###

# @name Instance-Info
# Returns an HTML page of info for the current instance of uibuilder
GET http://{{host}}/{{redAdminPath}}{{uibAdminPath}}/instance/{{url}}
content-type: {{contentType}}

{
    "cmd": "showinstancesettings",
    "time": "{{createdAt}}"
}

###

# @name List-All-User-Urls
# Returns json list of all ExpressJS routes on the user app
GET http://{{host}}/{{redAdminPath}}/{{uibPath}}/{{uibAdminPath}}/{{url}}
content-type: {{contentType}}

{
    "cmd": "listurls",
    "time": "{{createdAt}}"
}

// NOTES:
//  - can send a file: `< filename.json` or (for processing variables): `<@ filename.json`
//  - ExpressJS supported methods are here: https://expressjs.com/en/4x/api.html#app.METHOD
1 Like

Nice info, Julian -- and if you are familiar with using Postman, the ThunderClient extension does the same things, but from within your VSCode session.

I like to keep each project/server in a Collections of API calls that follow a logical testing sequence (login, query, update, create, etc) that I can quickly click through to test end-to-end. Various outputs of one API call (e.g. the login access_token) can be used by the next API call (e.g. add a Bearer token to the headers). And the call can be converted to curl or many other programming language constructs -- now if only it would output the JSON for a node-red http request node... hmmm :thinking:

ThunderClient looks interesting - I note that REST Client was an inspiration for it. As it happens, I prefer the text format of REST client over a GUI for this use. And I don't need CURL at all with these tools. I only use CURL to test downloads.

I did used to use Postman but then I had a series of issues with it - can't remember the details now - and I gave up on it. I think I found it too bloated and far too complex for ordinary use.

The ability to keep your tests in a single file is ideal - so for uibuilder for example, I have a tests folder and I keep all of the tests shown in a single file in there. I can run individual tests from within the file. But I think the new notebook format may be even better, not yet had the chance to try it extensively though.

I've also started using Jupyter notebooks to collect ideas, code snippets, etc not only for Python but also JavaScript since you can run notebooks on different "kernels" - also really handy to keep everything together along with Markdown based documentation. I use the same for when I need to work out a transformation process on data that I might want to redo later. Running up a Jupyter server for that used to be a major pain but with VSCode, that is no longer needed. That also meant I could ditch Anaconda which is not only horribly complex but is also no longer free in some cases.

This topic was automatically closed after 30 days. New replies are no longer allowed.