title: How to create a session for authentication with fetch API in JS
body: <blockquote>
  <p>I have two URLs. The first one is for login post request and the second URL is to call for a json. I can not get the json file from URL, if I am not logged in. How can I create a session so I can do more than one post request or alternatively how can i use JWT or set-cookie for authentication?</p>
  </blockquote>
  <pre><code>const fetch = (...args) =&gt; import('node-fetch').then(({default: fetch}) =&gt; fetch(...args));
  const login_url = &quot;https://www.example.com/login&quot;
  const json_url = &quot;https://www.example.com/users/anastasia&quot;
  const data = {&quot;username&quot;: &quot;my_username&quot;, &quot;password&quot;: &quot;my_password&quot;}
  const otherParm = {
     headers:{&quot;content-type&quot;: 'application/json'},
     body:JSON.stringify(data),
     method:&quot;POST&quot;
  };
  fetch(login_url, otherParm)
  //.then(data=&gt;{return data.json()})
  .then(res=&gt;{
      console.log(res.headers.get('jwt'))
     })
  .catch(err=&gt;console.log(err))
  </code></pre>
  <blockquote>
  <p>The response is:
  distinct_id: 156841653654e68w,
  jwt: e1616er5hw16e35hw6e5h6eth</p>
  </blockquote>
  <pre><code>fetch(json_url).then(function (response) {
      return response.json();
  }).then(function (obt) {
     console.log(obt);
  }).catch(function(error) {
     console.error('something went wrong with retrieving the json file');
     console.error(error);
  });
  </code></pre>
  <blockquote>
  <p>The response is: 401 Unauthorized</p>
  </blockquote>