1

I was using react native 0.48.3 and getting response from fetch request i don't see any encoding problem it resolves by itself the encoding problem because the content-type of my response is : application/json;charset=iso-8859-1 . Now i upgraded my react native app to 0.59.8 i don't know why fetch doesn't resolve encoding problem anymore althought it's the same code . I have just upgraded my app . Do you have any idea ? Here is my fetch code :

export const setDocumentListDataAsync = (k, action, server) => {
 return () => {
  fetch(defineUrlForDocumentList(action, server), {
    credentials: 'include',
    headers:{
      contentType: "application/json; charset=utf-8",
    }
  })
  .then(
  (response) => {
    var contentType = response.headers.get('content-type')
    console.warn(contentType)
      return response
    }
    ).then((response) => {
      return response.json()
    }).catch((err) => {
      console.log(err)
    })
  }
}
ksav
  • 20,015
  • 6
  • 46
  • 66
stuudd
  • 329
  • 2
  • 9
  • 16

3 Answers3

1

You can use iconv-lite to deal with the iso-8859-1 encoding. Since you need to fetch an ArrayBuffer and React Native doesn't support response.arrayBuffer() you can use the XMLHttpRequest API as a workaround.

Here is an example:

import iconv from 'iconv-lite';
import { Buffer } from 'buffer';

const fetchJSON = (url) => {
    return new Promise((resolve, reject) => {
        const request = new XMLHttpRequest();

        request.onload = () => {
            if (request.status === 200) {
                resolve(JSON.parse(iconv.decode(Buffer.from(request.response), 'iso-8859-1')));
            } else {
                reject(new Error(request.statusText));
            }
        };
        request.onerror = () => reject(new Error(request.statusText));
        request.responseType = 'arraybuffer';

        request.open('GET', url);
        request.setRequestHeader('Content-type', 'application/json; charset=iso-8859-1');
        request.send();
    });
}

export const setDocumentListDataAsync = (k, action, server) => {
    return () => {
        return fetchJSON(defineUrlForDocumentList(action, server))
            .catch(error => console.log(error));
    }
}

You should also install the packages buffer and stream.

Nico0302
  • 41
  • 1
  • 4
0

I think you have to do this :

export const setDocumentListDataAsync = (k, action, server) => {
 return () => {
  fetch(defineUrlForDocumentList(action, server), {
    credentials: 'include',
    headers:{
      contentType: "application/json; charset=utf-8",
    }
  })
  .then(
  (response) => {
    var contentType = response.headers.get('content-type')
    console.warn(contentType)
      return response.json()
    }
    ).then((myJson) => {
      console.log(JSON.stringify(myJson));
    }).catch((err) => {
      console.log(err)
    })
  }
}
Mahdi
  • 1,355
  • 1
  • 12
  • 28
-1

Might be an old question, but this issue can be easily solved using the arrayBuffer() function of the fetch() response. Had a similar issue, where the server returns data in ISO encoding. Fetch .text() automatically converts the response to UTF-8 and that leads to encoding issues.

Full solution described here: https://schneide.blog/2018/08/08/decoding-non-utf8-server-responses-using-the-fetch-api/

Andre
  • 131
  • 1
  • 14