Invalid header field name, with 128

370 views
Skip to first unread message

Valeh Asadli

unread,
May 14, 2020, 7:05:21 PM5/14/20
to Flutter Development (flutter-dev)
Hi guys, I got a problem with the header. 
> Invalid header field name, with 128


I'm using an iOS simulator. And if I not mistaken iOS simulator works well with localhost, in general, I have a information emulator that cannot access the localhost. That's why I have a decision it is maybe a bug. 

When I use another header such as
```dart
headers: {
   HttpHeaders.contentTypeHeader: 'application/json'
},
```
After that, I got different errors like that.

> flutter: Bad state: Cannot set the body fields of a Request with content-type "application/json".

One important thing FYI, _when I use the JSON MOCK SERVER, it works very well_, after switching DOCKER environment I got the error. I would like to add other details. I also test my real back-end code which is works on the docker environment, **I test on the browser with fetch API, and also angular application works well**. _I think that there is not related to my back-end or such kind of CORS issues._

 ```dart
 Future<void> login(String email, String password) async {
    try {
      final http.Response response = await http.post(
        url,
        body: {
          'email': email,
          'password': password,
          'device_name': 'apple',
        },
      );

      if (response.statusCode != 200) {
        throw HttpException('An error occored ${response.statusCode}');
      }

      final user = json.decode(response.body);
      _token = user['data']['token'];
      _email = email;

      final SharedPreferences sharedPreferences =
          await SharedPreferences.getInstance();
      final userData = json.encode({
        'token': _token,
        'email': _email,
      });
      sharedPreferences.setString('userData', userData);

      notifyListeners();
    } catch (exception) {
      throw exception;
    }
  }
```

Valeh Asadli

unread,
May 14, 2020, 7:07:50 PM5/14/20
to Flutter Development (flutter-dev)

Hayi Nukman

unread,
May 14, 2020, 7:15:38 PM5/14/20
to Valeh Asadli, Flutter Development (flutter-dev)
try to encode the request body into json.
body: jsonEncode({'email': email,'password': password,'device_name': 'apple',})

don't forget to import import 'dart:convert';

Regards.

Hayi Nukman

Just another IT freaks.
===================================

Umar ibn Abdul Aziz, may Allah have mercy on him, wrote to the people of Medina, “Whoever worships without knowledge will cause more harm than good. Whoever considers his words part of his deeds will speak little unless it concerns him. Whoever makes the purpose of his religion to indulge in arguments, he will frequently change his position.” - Sunan al-Dārimī 308


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/01b480fb-794a-4cef-aa64-6e56288ea054%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages