Correctly display special characters in JSON API response using nodeFetch

Viewed 2892

I have a react app that communicates with a RESTful API over HTTP. The API returns a JSON response body that may contain special characters.

An example response may look like this:

{
    "name": "Property One",
    "description": "Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) ±100ppm/°C Pad SMD Automotive T/R",
    ...
}

With the special characters in question being (but not limited to) ± and °.

I perform the request using nodeFetch like so:

nodeFetch(url, { method: 'GET', headers }).then(response => response.json());

If I observe the object after it has been deserialised using response.json(), the description property now looks like this:

"Res Thick Film 0805 49.9 Ohm 1% 0.25W(1/4W) �100ppm/�C Pad SMD Automotive T/R"

How do I stop the special characters getting replaced with �?

2 Answers

You probably should set the the header: Content-Type to text/html; charset=utf8. nodeFetch supports UTF-8 out of the box

Related