dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.25k stars 1.58k forks source link

HttpRequest.getString() recommended by docs for fetching JSON, but demands "text/" mime response #21578

Open Valloric opened 10 years ago

Valloric commented 10 years ago

Headline doc entry for HttpRequest: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:html.HttpRequest

> The simplest way to get the contents of a text file, such as a JSON-formatted file, is with getString. [example follows]

Docs for getString (same page):

> The server response must be a text/ mime type for this request to succeed.

Official mimetype for JSON: http://en.wikipedia.org/wiki/JSON#MIME_type

> The official MIME type for JSON text is "application/json".

These three facts put together make zero sense. Either getString should work with responses that aren't "text/" (makes the most sense), or the docs shouldn't recommend the use of this method for JSON.

kasperl commented 10 years ago

Added Area-Library, Library-Html, Triaged labels.

Valloric commented 10 years ago

This Dart tutorial also uses getString to fetch JSON data: https://www.dartlang.org/docs/tutorials/fetchdata/

alan-knight commented 9 years ago

The docs for getString are incorrect. It will happily succeed even if the content type is image/png and give you a bunch of characters. I'm not sure which encoding it's applying, but it should be fine for JSON text.


Removed Type-Defect label. Added Type-Documentation, Accepted labels.

dvorapa commented 2 years ago

I'm currently getting error to console as getString now expects XML somehow by default

dvorapa commented 2 years ago

It seems all is either responseType fault or browser bug. See #47743 where I describe my issue (pretty similar though)