br-g / openf1

OpenF1 API - Real-time and historical Formula 1 data
Other
380 stars 18 forks source link

Special Characters Issue When Consuming API with Flutter/Dart #11

Open xsampedro opened 8 months ago

xsampedro commented 8 months ago

Issue Description

I've encountered an encoding issue when consuming the web service using the Flutter/Dart HTTP client. The client seems to default to latin1 encoding when the Content-Type header lacks a specified charset, resulting in misinterpreted special characters.

Impact

This issue impacts any client that relies on the Content-Type header to determine the correct character encoding. Since the Flutter/Dart HTTP client defaults to latin1, it incorrectly decodes responses that include special characters, assuming the absence of a charset=utf-8 in the Content-Type header.

Steps to Reproduce

  1. Consume the API endpoint using Flutter/Dart HTTP client.
  2. Notice special characters are not displayed correctly.

Proposed Solution

To resolve this issue, I suggest explicitly setting the charset in the Content-Type header for JSON responses. This can be implemented in the _process_api_query function.

from fastapi.responses import JSONResponse

# Modify the _process_api_query function to return a JSONResponse with charset=utf-8
if not use_csv:
    return JSONResponse(content=results, media_type="application/json; charset=utf-8")
else:
    # Existing CSV handling logic

By specifying charset=utf-8 in the Content-Type header, clients like Flutter/Dart should correctly interpret the response encoding as UTF-8.

PrestonHager commented 1 month ago

@xsampedro has your issue been fixed with the release of v1.0? I have not seen issues on my projects yet.

vitoramaral10 commented 14 hours ago

Hello!

I'm having the same problem with the GP São Paulo meeting, for my Flutter App showing São Paulo.

I fixed it using utf8.decode(json['meeting_name'].runes.toList()),