SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend
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.
Request:
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());
});
Output in Console:
SocketException: OS Error: Connection refused, errno = 111
I Expect the response of this request to be a Json object containing the user and token.
Harnish, need a few more details in-order to debug this error.
- Are you running the server locally or communicating with a remote server?
- Are you running the app on the Android Emulator?
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
I went to the terminal and used ifconfig
command
I got my IP address: 192.168.0.109
Then I simply replaced this IP address with my "localhost" in flutter app http requests. And it worked like a charm!
In addition to Rohan's answer you can also read:
Unable to make calls to Localhost using Flutter, random port being assigned to HTTP GET call
I was trying to run my app on physical device using local server so, for that i had to use the below mentioned command, here keep in mind change the port number according to your own port number
adb reverse tcp:3001 tcp:3001
As @maheshmnj pointed that this command is redirecting your phone's port 3001 to your computer's port 3001. In case if you want to redirect port 2000 of your phone to port 3000 of your computer then you could do this
adb reverse tcp:2000 tcp:3000
you can also read about adb and networking via going through below mentioned page https://developer.android.com/studio/command-line/adb.html
- if in localhost instead localhost use the IP of your network
- hint:for find Ip in cmd run
ipconfig
and get the ipv4
1: Example:
await http.post("http://127.68.34.23/wp-json/...",
headers: headers,
body: json.encode(data))
if in server write the url of main in url