0

i'm trying to do a post request but all the time i get this error

Uncaught Error: XMLHttpRequest error.

i' working on dartpad on line and i'm using the package http.dart .I don't get the problem despite i post a json format i dont understand why the error is with the xml ?! this my code :

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

void   main() async {
    // This will be sent as form data in the post requst
    var map = new Map<String, dynamic>();
    map['username'] = '**********';
    map['password'] = '**********';

    final response = await http.post(
        Uri.parse('************'),
        body: map,
    );

    print(response.body);
}
  • Does this answer your question? [Dart/Flutter: Http request raises XMLHttpRequest error](https://stackoverflow.com/questions/71157863/dart-flutter-http-request-raises-xmlhttprequest-error) – eamirho3ein Aug 29 '22 at 09:04

1 Answers1

0

I suggest the following:

  1. Verify that your URI is correct, I tried your code on DartPad with a simple GET request onto a Free API I found on the web and I get no errors
  2. Verify that your endpoint has no weird CORS errors with another client (e.g. postman or Dio, see below)
  3. Verify that you don't have weird cache values onto your machine. Doing a flutter clean && flutter pub get might help
  4. Consider using Dio (pub.dev) as your http client as the dart's inner http package isn't quite ready for production imho.
venir
  • 1,809
  • 7
  • 19