Introduction

The Modelit Embedded Webserver Toolbox (MEWT) makes it possible to use Matlab as a server. One of the MEWT use-cases is to build a web app with a back-end implemented in Matlab. Either in compiled form but also as .m code in the Matlab IDE. When running the back-end of the web app directly in Matlab it is possible to apply break point debugging while using the site. This very effective debugging method.

Debugging web apps in their entirety can be tricky as different components are involved and the communication between these components typically is asynchronous. A tool like Postman can be used to display the data traffic between  the various components of a web app.  The current article shows how to set up and use Postman for debugging Matlab based web applications. 

Installation

A very useful tool to debug and test your Matlab back-end is Postman. (www.postman.com). Go to their site and install the Postman desktop app. After installing the Postman desktop app can be run by clicking on the desktop icon. The following screen appears:

Postman.png

Application to example "demoserver.m"

Now we will start the Matlab back-end by using the demo provided with the Modelit Embedded Server toolbox. 
Type the following command in the Matlab console:

demoserver

 This starts the server with a number of examples

Launching example "HTMLCallback" ...
To verify this example, open the url "localhost:8082/html" in your web browser
Launching example "XMLCallback" ...
To verify this example, open the url "localhost:8082/xml" in your web browser
Launching example "JSONCallback" ...
To verify this example, open the url "localhost:8082/json" in your web browser
Launching example "PDFCallback" ...
To verify this example, open the url "localhost:8082/pdf" in your web browser
Launching example "ImageCallback" ...
To verify this example, open the url "localhost:8082/image" in your web browser
Launching example "FigureCallback" ...
To verify this example, open the url "localhost:8082/figure" in your web browser
Launching example "AuthenticationCallback" ...
To verify this example, open the url "localhost:8082/authentication" in your web browser. Hint: username="user", password="1234".
Launching example "HTMLCallback with credentials" ...
To verify this example, open the url "localhost:8082/html2" in your web browser

For example: Sending a 'GET' request to localhost:8082/json results in a json message.
To call the running example define a new query in the Postman window by using the plus button:
PostmanPlusButton

Specifiy the Request method as 'GET'
and the URL as 'http://localhost:8082/json' (as appeared in the Matlab console when httpsServerExample was executed).
Click on the 'SEND' button to send the request to Matlab and in the lower half of the window the json result will appear.

PostmanJSONRequest

In order to debug the code that produces the json message, go to the JSONCallback.m file in the examples directory and open the file.
You can insert a breakpoint in the callback that is triggered when a request is sent to localhost:8082/json. For example on line 41 of this .m file.

MatlabBreakPoint

Now when sending the same request again from PostMan Matlab stops at this point and the 'event' argument, which contains all information specified in PostMan can be inspected.

Advanced topics

For this simple example no arguments were specified in the PostMan Http query, but it is possible to specify query parameters, header values and a body in PostMan, These values can be spccified in the Params, Body and Headers tab. And can be inspected in Matlab by using the modelit.web.client.HttpRequest methods: getRequestHeaders, getRequestBody, getQueryString and getQueryValue.