Search code examples
ajaxdebuggingxmlhttprequestresponsemonitoring

Monitor network request URL and response content (Throw custom exceptions)


How can network requests be monitored and evaluated for their request URL, parameters, request, and response data?

Desired solution

I want to be notified or being given a custom exception, if specific content occurs in request or response.

Example

Assume a web application with many dynamic Ajax requests. A request or response might contain a broken value, e.g. undefined.

Request URL:

http://localhost:8080/app/?undefined=1

Response JSON data:

{"undefined":"1"}

Attempts

  • Filtering for request content in Dev Tools is not possible
  • PostMan tests seem not viable (e.g. no user interactions)

Not tried/found yet

Guesses of what might work ...

  • Software to intercepts requests and log/alert details
  • Proxying any URLs on an OS via some standalone application

Solution

  • If importing axios, you could leverage custom interceptors:

    // Add a response interceptor
    axios.interceptors.response.use(function (response) {
        // Any status code that lie within the range of 2xx cause this function to trigger
        // Do something with response data
        return response;
      }, function (error) {
        // Any status codes that falls outside the range of 2xx cause this function to trigger
        // Do something with response error
        return Promise.reject(error);
      });
    
    
    // Similar functionality can be achieved for request
    axios.interceptors.request.use(function (config) {
        // Do something before request is sent
        return config;
      }, function (error) {
        // Do something with request error
        return Promise.reject(error);
      });