<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eşref Ali Ünal on Medium]]></title>
        <description><![CDATA[Stories by Eşref Ali Ünal on Medium]]></description>
        <link>https://medium.com/@esrefali?source=rss-71c4b5ba1960------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Eşref Ali Ünal on Medium</title>
            <link>https://medium.com/@esrefali?source=rss-71c4b5ba1960------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 03:24:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@esrefali/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Axios Interceptors]]></title>
            <link>https://medium.com/@esrefali/axios-interceptors-17822e7891b9?source=rss-71c4b5ba1960------2</link>
            <guid isPermaLink="false">https://medium.com/p/17822e7891b9</guid>
            <category><![CDATA[interceptors]]></category>
            <category><![CDATA[axios]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Eşref Ali Ünal]]></dc:creator>
            <pubDate>Thu, 25 Jan 2024 21:20:59 GMT</pubDate>
            <atom:updated>2024-01-25T21:20:59.417Z</atom:updated>
            <content:encoded><![CDATA[<p>How can we gain control over Axios requests or responses? Doing this with interceptors is a breeze!</p><h4>What Are Interceptors?</h4><p>Applications are generally based on data transfer. Axios is one of the reliable libraries we can use in on this content. We can use interceptors to overcome the problems which encounter during the API requests. Interceptors allow us to easily perform operations such as editing, error handling on HTTP flows. We can think of interceptors as a middle layer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*26xIRqGVOMjUwLqFUMrVCw.png" /></figure><p>Let’s talk about how interceptors are used!</p><p>Suppose you want to add a token for authorization to the header information of all requests:</p><pre>const apiCaller = Axios.create({<br>  baseURL: process.env.serverEndpointUrl<br>})<br><br>apiCaller.interceptors.request.use((config) =&gt; {<br>  const authorizationToken = getToken()<br>  if (authorizationToken) {<br>    config.headers.authorizationToken = authorizationToken<br>  }<br>  <br>  return config<br>})</pre><p>What if we wants to add a timeout:</p><pre>const apiCaller = Axios.create({<br>  baseURL: process.env.serverEndpointUrl<br>})<br><br>apiCaller.interceptors.request.use((config) =&gt; {<br>  config.timeout = 1000 // ms<br><br>  return config<br>})</pre><p>Or a simple data:</p><pre>const apiCaller = Axios.create({<br>  baseURL: process.env.serverEndpointUrl<br>})<br><br>apiCaller.interceptors.request.use((config) =&gt; {<br>  config.data = {<br>    requestParameters: {<br>      name: &#39;fooName&#39;,<br>      address: &#39;fooAddress&#39;<br>    }<br>  }<br>  return config<br>})</pre><p>Of course, you can apply data manipulation or error handling on responses as well as request. If there is a specifically expected format in your project, you can manage the response. You can also perform different actions for each error type.</p><pre>axios.interceptor.response.use((response) =&gt; {<br>  response.data = response.data.map(info =&gt; ({<br>    address: info.addr,<br>    country: info.cntr<br>  }))<br>  return response;<br>},<br>(error) =&gt; {<br>  if (error?.response?.status === &#39;404&#39;) {<br>    // handle error bad request<br>  } else if (error?.response?.status === &#39;500&#39;){<br>    // handle error internal server<br>  } else {<br>    // handle error the others<br>  }<br><br>  return Promise.reject(error);<br>})</pre><p>In this small article, I tried to explain what interceptors are and how they are used. I hope it was useful, thanks for your time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17822e7891b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>