Automating Data Capture with Interceptors

Axios Interceptor

To automatically capture data from Axios requests, you can use the interceptAxiosRequest function. This will monitor requests made by Axios and send the tracked data to the server.

Example for Axios

const axios = require('axios');
const { interceptAxiosRequest } = require('@handit.ai/node');

const axiosInstance = axios.create();
interceptAxiosRequest(axiosInstance);

// Example Axios request
axiosInstance.post('/api/model', { input: 'inputData' })
  .then(response => {
    console.log(response.data);  // The request and response are tracked automatically
  });

Fetch Interceptor

Similarly, you can track requests made using the Fetch API by using the interceptFetchRequest function.

Example for Fetch

const { interceptFetchRequest } = require('@handit.ai/node');

interceptFetchRequest();

// Example Fetch request
fetch('/api/model', {
  method: 'POST',
  body: JSON.stringify({ input: 'inputData' }),
})
  .then(response => response.json())
  .then(data => {
    console.log(data);  // The request and response are tracked automatically
  });

Explanation

  • interceptAxiosRequest and interceptFetchRequest automate the tracking of HTTP requests. They capture both the request body and response data for tracking purposes.

  • Once set up, every HTTP request made using Axios or Fetch is monitored and tracked automatically without any additional manual intervention.

Last updated