Dart Language: encoding (related to HttpRequest and http_server package)

135 views Asked by At

I'm creating a file and writing a String on it with encoding set to LATIN1. However, the finished file is set with a different encoding (us-ascii or utf-8 returned by "file -bi" on Linux, depending on the method I use to get the String).

Here follows the creation method:

new File("/home/username/dart_test/file.xml").create(recursive: true).then((file) {
    file.writeAsString(_methodReturnsAString(), mode: FileMode.WRITE, encoding: LATIN1);
});

Any ideas on what could be wrong?

EDIT (RELATED TO ANSWER):

There's no problem on the method described above. The problem was the data that was being provided to the method inside "writeAsString". That data comes from an HttpRequest that was not being processed properly (in fact, the setting of the encoding to ISO-8859-1 was causing the problem).

1

There are 1 answers

0
Felipe On

There's no problem with the method described on the question. Actually, the problem lays in an http request body handler that was not set properly.

So I'm answering my own question in order to help others with the same problem.

Here follows my request handler (from http_server package):

    HttpBodyHandler.processRequest(request/*, defaultEncoding: Encoding.getByName("ISO-8859-1")*/).then((body) {
        // Do something with body.
    }, onError: _printError);

Take a look at the commented out "defaultEncoding". That was the cause. I don't think you can set it if you are not processing any files (blobs) on the request. I don't know if there's any situation where you should set it when just processing some String (I would appreciate if someone could complete this answer with this information).