I m building a flutter app with django rest-framework. The registration api is working fine in Postman but after some successful registration from the flutter app it is showing the above error. The request is been sent on https address.
Removed csrf. Nothing happens.
var data = {'email':signupemailidcontroller.text, 'password1':passwordcontroller.text, 'password2':confirmpasswordcontroller.text, }; //http request here await http.post(websitesignupurl, headers: headers, body: json.encode(data)) .then((onResponse){ print(onResponse.body); }).catchError((onerror){ print(onerror.toString()); });
SocketException: OS Error: Connection refused, errno = 111
I Expect the response of this request to be a Json object containing the user and token.
For example, the classic Connection refused errno 111 means that the initial SYN packet to the host you connect() to was responded with an RST packet instead of the normal SYN+ACK — which usually happens when there's no program listening to the given port on the remote computer you connect() to.
Flutter SocketException is thrown when a socket operation fails. Implemented types. IOException. Constructors.
Harnish, need a few more details in-order to debug this error.
Possible Solution:
If you're running the server locally and using the Android emulator, then your server endpoint should be 10.0.2.2:8000
instead of localhost:8000
as AVD uses 10.0.2.2
as an alias to your host loopback interface (i.e) localhost
Note on Futures
I noticed above that the code is using await and then on the same line. This can be confusing, to be clear, await
is used to suspend execution until a future completes, and then
is a callback function to execute after a future completed. The same could be written as below
void myFunction() async { var data = {}; var response = await http.post(URL, headers:headers, body:data); if (response.statusCode == 200) { print(reponse.body); } else { print('A network error occurred'); } }
or the non async/await method
void myFunction() { var data = {}; http.post(URL, headers:headers, body:data) .then((response) => print(response.body)) .catchError((error) => print(error)); }
For a more detailed information on Futures in Dart please read https://www.dartlang.org/tutorials/language/futures
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With