<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tajul Islam Rifat</title>
    <description>The latest articles on DEV Community by Tajul Islam Rifat (@refath).</description>
    <link>https://dev.to/refath</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F647865%2F67f0552e-7a05-4a17-844f-0f766e3fa72a.png</url>
      <title>DEV Community: Tajul Islam Rifat</title>
      <link>https://dev.to/refath</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/refath"/>
    <language>en</language>
    <item>
      <title>Axios interceptors For ReactJs</title>
      <dc:creator>Tajul Islam Rifat</dc:creator>
      <pubDate>Mon, 22 May 2023 14:53:03 +0000</pubDate>
      <link>https://dev.to/refath/create-axios-interceptors-for-reactjs-6j0</link>
      <guid>https://dev.to/refath/create-axios-interceptors-for-reactjs-6j0</guid>
      <description>&lt;p&gt;Interceptors allow you to modify the request or response before it is sent or received by the server. Interceptors are useful because they allow developers to add custom functionality to requests and responses without modifying the actual code that makes the request.&lt;/p&gt;

&lt;p&gt;Install Axios in your Next.js project. Open your project directory in the terminal and run the following command&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install axios&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create a new file (e.g., axiosInstance.js) to configure your Axios instance and define your interceptors&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axios from 'axios';
const axiosInstance = axios.create({
  baseURL: 'http://api.example.com', // Replace with your API base URL
});

// Request interceptor
axiosInstance.interceptors.request.use(
  (config) =&amp;gt; {
    // Modify the request config here (add headers, authentication tokens)
        const accessToken = JSON.parse(localStorage.getItem("token"));

    // If token is present add it to request's Authorization Header
    if (accessToken) {
      if (config.headers) config.headers.token = accessToken;
    }
    return config;
  },
  (error) =&amp;gt; {
    // Handle request errors here

    return Promise.reject(error);
  }
);

// Response interceptor
axiosInstance.interceptors.response.use(
  (response) =&amp;gt; {
    // Modify the response data here
    return response;
  },
  (error) =&amp;gt; {
    // Handle response errors here

    return Promise.reject(error);
  }
);

export default axiosInstance;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we create an Axios instance using &lt;strong&gt;&lt;code&gt;axios.create&lt;/code&gt;&lt;/strong&gt; and set the &lt;strong&gt;&lt;code&gt;baseURL&lt;/code&gt;&lt;/strong&gt; to your API's base URL. You can modify this according to your API configuration.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;interceptors.request.use&lt;/code&gt;&lt;/strong&gt; function is used to intercept and modify outgoing requests. You can add headers, authentication tokens, or perform other modifications to the request configuration.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;interceptors.response.use&lt;/code&gt;&lt;/strong&gt; function is used to intercept and modify incoming responses. You can parse, transform, or handle errors in the response.&lt;/p&gt;

&lt;p&gt;Now, you can use the axiosInstance in your components to make API requests with the configured interceptors. For example, in your component file (example.js)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import axiosInstance from '../axios/axiosInstance';

function ExamplePage() {
  const fetchData = async () =&amp;gt; {
    try {
      const response = await axiosInstance.get('/api/data'); // Replace with your API endpoint

      // Handle the response data here
      console.log(response.data);
    } catch (error) {
      // Handle the error here
      console.error(error);
    }
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Example Page&amp;lt;/h1&amp;gt;
      &amp;lt;button onClick={fetchData}&amp;gt;Fetch Data&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default ExamplePage;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, we import the &lt;strong&gt;&lt;code&gt;axiosInstance&lt;/code&gt;&lt;/strong&gt; we defined earlier and use it to make API requests. You can replace &lt;strong&gt;&lt;code&gt;/api/data&lt;/code&gt;&lt;/strong&gt; with the appropriate endpoint URL for your API.&lt;/p&gt;

&lt;p&gt;By using &lt;strong&gt;&lt;code&gt;axiosInstance&lt;/code&gt;&lt;/strong&gt;, the requests made from your project components will automatically go through the defined interceptors, allowing you to modify the requests and responses as needed.&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>axios</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
