dart-lang / http

A composable API for making HTTP requests in Dart.
https://pub.dev/packages/http
BSD 3-Clause "New" or "Revised" License
1.03k stars 359 forks source link

Error: Connection closed before full header was received #577

Open gutisalex opened 3 years ago

gutisalex commented 3 years ago

Hey I am facing a really weird problem I am not able to solve...

I wrote a client for a wifi router I am working with right now. I am using the bloc architecture for this. So I have my bloc which asks a repository for certain data which is calling methods from the routerApiClient.

In the routerApiClient I import http like this:

import 'package:http/http.dart' as httpClient;

and use

httpClient.post(...)

that works!

But if I import http like this:

import 'package:http/http.dart' as http;

and instantiate the http client like this:

class RouterApiClient {
  final http.Client _httpClient;

  RouterApiClient({http.Client httpClient})
      : _httpClient = httpClient ?? http.Client();

and use

_httpClient.post(...)

the first call works fine but with the second call (which is called right after the first one) I get the following error:

Connection closed before full header was received

Here are the calls:

Future<void> init(String username, String password) async {

    await _httpClient.post(`$base_url/login`, {'user': username, 'pass': password});

    final loginState = await _httpClient.get(`$base_url/login-state`);

    if (loginState == 1) {
      print('is authenticated');
    } else {
      print('is not authenticated');
    }
  }

I have another provider which fetches data from an API from a remote server using the exact same approach and for some reason there it works... The only difference is the wifi router uses http and the webserver uses https.

It looks like the calls are being triggered too fast one after another, because after this has failed I can do other calls as normal.

I only don't know why it works if I use the import alias directly and why it doesn't when instantiating the httpClient in the constructor?!

Any ideas?

Edit: I can confirm they are being trigged too fast one after another. I just put a timeout in between and it works. Can I solve this in another way?

LailaiMaster commented 1 year ago

+1

ryanaltair commented 11 months ago

+1

ryanaltair commented 11 months ago

In our test, there's no different between import 'package:http/http.dart' as httpClient; or RouterApiClient({http.Client httpClient}) : _httpClient = httpClient ?? http.Client();, when we post to mush post http request at the same time, the errors still going on, no matter with instantiate or not.