Dart character encoding in http request

Issue

Just learning Flutter and running into this issue when trying to call an API:

final response = await http.get(
  Uri.https(apiBaseUrl, apiBaseEndpoint + "/tasks"),
  headers: {
    "Authorization": "Bearer " + apiKey,
  },
);

print(response.body);

Part of my response contains Ä°ftar and it’s supposed to be İftar. I imagine it’s some encoding problem? curl gives me back the response with the proper characters.

Basically: is this a text encoding problem? If so, how do I fix my request?

Solution

Ok, after a little bit more digging on the http docs I realized it wasn’t in how I made my request that needed to change, but how I handled the response. I was doing

final decodedJson = json.decode(response.body);

and I should’ve been doing:

final decodedJson = json.decode(utf8.decode(response.bodyBytes));

That has solved my issue!

Answered By – derekantrican

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.