Debugging Web
Tags: frontend, soap, tutorial, debuggin
Published: 2023-05-18 15:49:33

In this article I'll cover testing and debugging Caché web applications (mainly REST) with external tools. [Second part](https://community.intersystems.com/post/debugging-web-part-2) covers Caché tools.

You wrote server-side code and want to test it from a client or already have a web application and it doesn't work. Here comes debugging. In this article I'll go from the easiest to use tools (browser) to the most comprehensive (packet analyzer), but first let's talk a little about most common errors and how they can be resolved.

### Errors

**401 Unauthorized**

I think it's the most often encountered error during the deploy to production. Local development server usually has minimal security setting or normal but vanilla security. Production server, however can have a more restrictive scheme. So:

  * Check that you're logged in
  * Check that user has access to the database/table/procedure/row/column you want to access
  * Check that OPTIONS request can be performed by unauthorized user

**404 Not Found**

Check:

  * Url is correct
  * If it's a new application and you're using external web-server reloading web-server can help

**Application errors**

In some way the easiest to find - stack trace helps. Resolution is completely application-specific.

### Debugging tools

**Web browser**

The first and always available debug tool is a Web browser, preferably Chrome but Firefox would also suffice. GET requests can be tested by entering the URL into address bar,  all other requests require a web application or writing js code. General approach is:

  * Press F12 to open developer tools.
  * Go to Network tab
  * Check Preserve Log checkbox, if not set
  * Display only XHR requests
  * Perform buggy action in web application


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2g6l242n0wyl2jfn1g4f.png) 

From here you can examine requests and resend them. Firefox can also edit requests before repeating them.

Pros:

  * Always available
  * Easy to use (end users can send screenshots of Network and Console tabs)
  * It's end user environment

Cons

  * Doesn't show partially send/broken/etc. responses
  * Slow on big responses
  * Slow on big number of responses
  * Everything is done manually

**REST client**

REST Client is a standalone web application or web browser addon made specifically for testing web applications. I use [Postman](https://www.getpostman.com/), but there are a lot of them. Here's how debugging on Postman looks like:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8gohpmnfmsscucr764vr.png)

Postman works with requests grouped into collections. Request can be sent into the environment. Environment is a collection of variables. For example in my <tt>CACHE@localhost</tt> environment host variable is set to localhost and user to _SYSTEM. When request is sent variables are replaced with their values for chosen environment and the request is sent.

Here's a sample [collection](https://github.com/intersystems-ru/Cache-MDX2JSON/blob/master/MDX2JSON.postman_collection.json) and [environment](https://github.com/intersystems-ru/Cache-MDX2JSON/blob/master/CACHE.postman_environment.json) for MDX2JSON project.

Pros:

  * Write once - use everywhere
  * Better control over request
  * Response prettification

Cons:

  * &lt;s>Debugging chained requests (response to request1 can force either request2 or request 2B) is still manual&lt;/s> (Feb 22 update: Possible in Postman)
  * Sometimes fails on partially send/broken/etc. responses

**HTTP Debugging proxy**

A standalone application that logs HTTP(S) traffic. Logged requests can be modified and resend. I use [Charles](https://www.charlesproxy.com/) and [Fiddler](http://www.telerik.com/fiddler).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzp9fhz0cms9aenr67m8.png) 

<p data-select-like-a-boss="1">
  Pros:
</p>

  * Processes partially send/broken/etc. responses
  * Response prettification
  * Better support for HTTPS traffic (than in packet analyzer)
  * Can save capture sessions

Cons:

  * Something (web application/REST client/JS code) is required to send the request

<p data-select-like-a-boss="1">
  <strong>Packet analyzer</strong>
</p>

A computer program that can intercept and log traffic that passes over a network. As data streams flow across the network, the sniffer captures each packet and, if needed, decodes the packet's raw data. This is the most comprehensive option, but also requires some skill to operate properly. I use [WireShark](https://www.wireshark.org/). Here's a little guide on how to install and use it:

  1. If you're going to capture local packets, read about [loopback](https://wiki.wireshark.org/CaptureSetup/Loopback) and install prerequisite software (npcap for windows)
  2. Install WireShark
  3. Configure [capture filters](https://wiki.wireshark.org/CaptureFilters) (for example filter to capture only traffic on 57772: <kbd>port 57772</kbd>
  4. Start capture
  5. Configure [display filters](https://wiki.wireshark.org/DisplayFilters) (for example filter to display only http traffic to a specific ip: <kbd>ip.addr == 1.2.3.4 && http</kbd>

Here's a sample capture of http traffic (display filter) on 57772 port (capture filter):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9n4s9l1xd49m4nu72bl.png)  

Pros:
</p>

  * Processes partially send/broken/etc. responses
  * Can capture large amounts of traffic
  * Can capture anything
  * Can save capture sessions

Cons:

  * Something (web application/REST client/JS code) is required to send the request

<h3 data-select-like-a-boss="1">
  What to use
</h3>

Well, that depends on a purpose. First of all we can aim to either log (debug proxy, packet analyzer) or generate (browser, REST client) requests.

If you're developing a REST Web API,  then REST client is the fastest way to test that it works.

If, however, requests from REST client work, but client web application does not, browser, http debug proxy and packet analyzer may be required.

If you have clients and you need to develop server-side api to work with them, you'll need http debug proxy or packet analyzer.

<p data-select-like-a-boss="1">
  It's better to be familiar with all 4 types of tools and quickly switch between them if the current one is insufficient for the job.
</p>

Sometimes the right tool is obvious.

For example recently I developed server side API for one popular http extension protocol, the requirements were:

  * Clients are already written and we can't change their code
  * Different clients behave differently
  * Behavior on http and https differs
  * Behavior with different authentication types differs
  * Up to hundred requests per second per client
  * Everyone ignores the RFC

There's only one solution here - packet analyzer.

Or if I'm developing a REST API for JS consumption, REST client is a perfect tool for testing.

When debugging web application start with web browser.

<p data-select-like-a-boss="1">
  <a href="https://community.intersystems.com/post/debugging-web-part-2">In Part 2</a> we'll discuss what can be done (a lot) for web debugging on the Caché side.
</p>

What are your approaches to debugging client-server communication?