Skip to main content

API Integration

Axios

https://github.com/axios/axios

Set Default Headers

This will set the content-type header for all Axios API requests made within the application.

axios.defaults.headers.put["Content-Type"] = "application/json";

Interceptor

Example of setting up a global API error handler, specifically to catch ServiceNow session timeout errors.

I’ve added this in App.js but the interceptor could also be defined in an API base class.

// NOTE: Setting undefined for the response handler allows all other response handlers to behave normally.

axios.interceptors.responses.use(
undefined,
(error) => {
if (error && error.response && error.response.status === 401) {
// This is a session timeout error.
// Take special action to handle the error such as opening a dialog to display the error message;
}

console.error(error);

// Return a rejected promise so individual error handlers will execute as expected.
return Promise.reject(error);
}
);