Dart http GET request fails when host returns HTTP status code 200

33 views Asked by At

Trying to make a simple (Dart Http) GET request with no query params. The host is a local docker machine. I have a JMeter test that shows the correct call and response. When the call is placed from Dart-http my apache log shows the api response as 200OK but dart http.get() throws an exception.

Dart http: 1.1.0

void main() async {

  Map<String, String> xhr_headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
  };
  final uri = Uri.http('sweeps.dock', '/api/sweepstakes');
  var response = await http.get(uri);
  var responseData = json.decode(response.body);
}

Error: ClientException: XMLHttpRequest error., uri=http://sweeps.dock/api/sweepstakes dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 941:28 get current

Exception is thrown from the browser_client.dart::send function where it claims to have no more information.

    unawaited(xhr.onError.first.then((_) {
      // Unfortunately, the underlying XMLHttpRequest API doesn't expose any
      // specific information about the error itself.
      completer.completeError(
          ClientException('XMLHttpRequest error.', request.url),
          StackTrace.current);
    }));

How do I get more information on the failure?

I tried setting the Accept header to see if I was getting a json response that the dart-http lib was not expecting. In this case, dart-http begins by sending the OPTIONS call. This also returns 200OK (apache logs) and I get the same exception.

var response = await http.get(uri, headers:xhr_headers);
1

There are 1 answers

1
Benas Petronis On

I believe this will fix your problem:

void main() async {

  Map<String, String> xhr_headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
  };
  final uri = Uri.http('sweeps.dock', '/api/sweepstakes');
}