title: nodejs - check if fetch response is json or text
body: <p>I'm new to coding and i'm using <code>node-fetch</code> and trying to check if the response is a <code>json</code> or <code>text</code> but i get an error if the response is text.</p>
  <p>If the response is a json it will work fine but if the response is text i get an error.</p>
  <p>The error i get:</p>
  <pre><code>invalid json response body at https://url.com/api reason: Unexpected token i in JSON at position 6
  </code></pre>
  <p>My code:</p>
  <pre><code>const fetch = require(&quot;node-fetch&quot;);
  
  const doFetch = async (url) =&gt; {
    try {
      let res = await fetch(url);
  
      try {
        return res.json();
      } catch (error) {
        return res.text();
      }
      
    } catch (error) {
      console.log('fetch error', error.message);
    }
  }
  </code></pre>