close

[Solved] fetch() unexpected end of input

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error fetch() unexpected end of input in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How fetch() unexpected end of input Error Occurs?

Today I get the following error fetch() unexpected end of input in javascript.

How To Solve fetch() unexpected end of input Error ?

  1. How To Solve fetch() unexpected end of input Error ?

    To Solve fetch() unexpected end of input Error
    const response = await fetch(url, { method: 'GET', headers: { Authorization: 'Bearer ' + accessToken } }); const string = await response.text(); const json = string === "" ? {} : JSON.parse(string); return json;

  2. fetch() unexpected end of input

    To Solve fetch() unexpected end of input Error
    const response = await fetch(url, { method: 'GET', headers: { Authorization: 'Bearer ' + accessToken } }); const string = await response.text(); const json = string === "" ? {} : JSON.parse(string); return json;

Solution 1

I had the same problem. in my case it wasn’t caused by the response type of ‘opaque’ as the solution pointed. This code cause an error with empty response, because ‘fetch’ doesn’t accept responses with empty body :

return fetch(urlToUser, parameters)
.then(response => {
  return response.json()
})
.then((data) => {
  resolve(data)
})
.catch((error) => {
  reject(error)
})

Instead, in my case this works better :

return fetch(urlToUser, parameters)
.then(response => {
  return response.text()
})
.then((data) => {
  resolve(data ? JSON.parse(data) : {})
})
.catch((error) => {
  reject(error)
})

Gettting the text doesn’t give the error even with the empty body. Then check if data exists and resolve. I hope it helps 🙂

Solution 2

Lots of good responses but I chose this:

      const response = await fetch(url, {
        method: 'GET',
        headers: {
          Authorization: 'Bearer ' + accessToken
        }
      });
      const string = await response.text();
      const json = string === "" ? {} : JSON.parse(string);
      return json;

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment