http icon indicating copy to clipboard operation
http copied to clipboard

Error: Connection closed before full header was received

Open gutisalex opened this issue 3 years ago • 3 comments

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?

gutisalex avatar May 26 '21 13:05 gutisalex

+1

LailaiMaster avatar Jan 17 '23 03:01 LailaiMaster

+1

ryanaltair avatar Dec 31 '23 15:12 ryanaltair

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.

ryanaltair avatar Dec 31 '23 15:12 ryanaltair