<?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: Muhamad Rahmat Setiawan</title>
    <description>The latest articles on DEV Community by Muhamad Rahmat Setiawan (@mrsetiawan).</description>
    <link>https://dev.to/mrsetiawan</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%2F338086%2Fc29a2ebb-b649-4df8-baba-18371b928dae.jpeg</url>
      <title>DEV Community: Muhamad Rahmat Setiawan</title>
      <link>https://dev.to/mrsetiawan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrsetiawan"/>
    <language>en</language>
    <item>
      <title>how to insert app-token into the header on axios and react js?</title>
      <dc:creator>Muhamad Rahmat Setiawan</dc:creator>
      <pubDate>Sat, 20 Jun 2020 18:08:23 +0000</pubDate>
      <link>https://dev.to/mrsetiawan/how-to-insert-app-token-into-the-header-on-axios-and-react-js-3a78</link>
      <guid>https://dev.to/mrsetiawan/how-to-insert-app-token-into-the-header-on-axios-and-react-js-3a78</guid>
      <description>&lt;p&gt;first I post the API to the "api / app-token" endpoint "The API doesn't need a header", after that the response from the "api / app-token" is stored in the localstorage with the app-token key, after saving it in the localstorage I take app- I then set the token to the header to post another API endpoint that requires a header, the problem is that the header cannot retrieve data from localstorage so the other endpoint cannot be accessed because the header has not been filled with app-tokens that are in localstorage, I try to outsmart it with force the app-token to a state when the stat is loaded then I make the page refresh "window.location.reload ()", but why isn't the function running? or is there a more efficient way?&lt;/p&gt;

&lt;p&gt;There are basically 2 endpoints, the first endpoint "api / app-token" does not need a header, then the second endpoint requires a header, and the header must be filled with app-tokens that I set into localstorage&lt;/p&gt;

&lt;p&gt;this is example axios post for get app-token&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [getAppToken, setgetAppToken] = useState({})
  const [company, setCompany] = useState({
    company_name: 'fevci',
    comp_key: 'fevci123'
  })

  useEffect(() =&amp;gt; {
    const getAppToken = async () =&amp;gt; {
      const res = await axios({
        method: 'post',
        url: endpoint + 'api/app_token',
        data: company,
      })
      const { data: appToken, success } = res.data

      if (success === true) {
        storage.setItem('app-token', JSON.stringify(appToken))
      } else {
        storage.removeItem('app-token')
      }

      return res
    }
    getAppToken()
  }, [])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and this is an example of axios which has a header assuming the header has been filled with app-tokens&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const api = axios.create({
  baseURL: endpoint,
  headers: {
    'app-token': (storage.getItem('app-token') !== null) ? JSON.parse(storage.getItem('app-token')) : null,
    'Authorization': (storage.getItem('token') !== null) ? JSON.parse(storage.getItem('token')) : null,
    'content-type': 'multipart/form-data'
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
