dart-lang / shelf

Web server middleware for Dart
https://pub.dev/packages/shelf
BSD 3-Clause "New" or "Revised" License
921 stars 124 forks source link

Response with accented text, returns file for download. #413

Closed denisales closed 7 months ago

denisales commented 7 months ago

I was creating a GET endpoint, and when returning the text Hello World in Portuguese, GET started opening a download window.

Is this behavior normal?

Dart SDK version: 3.2.3 (stable) on "windows_x64"

main.dart

import 'package:shelf/shelf_io.dart' as shelf_io;
import 'app_handler_server.dart';

void main() async {
  var serveHandler = AppHandlerServer();

  final server = await shelf_io.serve(serveHandler.handler, 'localhost', 8080);

  print('Server is running...');
}

app_handler_server.dart

import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';

class AppHandlerServer {
  Handler get handler {
    final router = Router();

    router.get('/', (Request req) {
       //Try returning the String Olá mundo with an accent and without an accent and see the difference.
      // return Response(200, body: 'Ola mundo');
      return Response(200, body: 'Olá mundo');
    });

    return router;
  }
}
denisales commented 7 months ago

I was creating a GET endpoint, and when returning the text Hello World in Portuguese, GET started opening a download window.

Is this behavior normal?

Dart SDK version: 3.2.3 (stable) on "windows_x64"

main.dart

import 'package:shelf/shelf_io.dart' as shelf_io;
import 'app_handler_server.dart';

void main() async {
  var serveHandler = AppHandlerServer();

  final server = await shelf_io.serve(serveHandler.handler, 'localhost', 8080);

  print('Server is running...');
}

app_handler_server.dart

import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';

class AppHandlerServer {
  Handler get handler {
    final router = Router();

    router.get('/', (Request req) {
       //Try returning the String Olá mundo with an accent and without an accent and see the difference.
      // return Response(200, body: 'Ola mundo');
      return Response(200, body: 'Olá mundo');
    });

    return router;
  }
}

Solved, I needed to put the header as: text/plain; charset=utf-8