Flutter HTTP request fails with Connection refused error errno 111 when calling Django REST API

I’m working on a Flutter mobile app that connects to a Django REST framework backend. My user registration endpoint works perfectly when I test it through Postman, but I’m getting connection issues when calling it from the Flutter app.

The weird thing is that a few registrations work fine at first, but then I start getting this socket error. I’m making requests to an HTTPS URL and already tried removing CSRF protection but that didn’t help.

My Flutter code:

var payload = {
  'user_email': emailInputController.text,
  'pass1': newPasswordController.text,
  'pass2': confirmPasswordController.text,
};

// Making the HTTP call
http.post(registrationApiUrl,
    headers: requestHeaders,
    body: json.encode(payload))
.then((response) {
  print(response.body);
}).catchError((error) {
  print(error.toString());
});

Error message:

SocketException: OS Error: Connection refused, errno = 111

I should be getting back a JSON response with user details and an authentication token. Any ideas what might be causing this intermittent connection issue?

errno 111 means your Django server is crashing or becoming unresponsive after a few requests. This happens all the time in development when the server crashes from unhandled exceptions or memory issues. Check your Django logs right after connections start failing - you’ll see error traces or the server just stopped running. I had this exact problem when my Django views had database connection leaks that built up over multiple requests. Fix the server-side issue instead of messing with your Flutter client code. Try restarting your Django server when you get the error to confirm this is what’s happening.

could be a rate limiting issue on django’s side. sometimes servers block rapid requests. maybe add a wee delay or check your throttling settings. and errno 111? that just means the server’s like “nope, not gonna let you in”.

that’s weird - works at first then crashes? are you testing on an emulator or real device? emulators can have funky network issues. what’s your registrationApiUrl set to - localhost or a real domain?