Reputation: 1246
Here is my code:
import 'package:http/http.dart' as http;
void getData() async {
print('getData called');
// Q is not a typo
String urlString = 'http://Qsamples.openweathermap.org/';
var url = Uri.parse(urlString);
http.Response response = await http.get(url);
if (response.statusCode == 200) {
print('statusCode = 200');
} else {
print('statusCode != 200');
}
}
When I make a request to the valid URL everything works like a charm. But when I change it to invalid I get
E/flutter (19828): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: SocketException: Failed host lookup: 'qsamples.openweathermap.org' (OS Error: No address associated with hostname, errno = 7)
instead of receiving response.statusCode == 404
.
Here there is an example from library's authors where they are advising the same https://pub.dev/packages/http/example
Upvotes: 2
Views: 660
Reputation: 166
Try to change your url from :
To :
it should return 404 error
Upvotes: 1
Reputation: 2425
the urlString you used is not working.The urlString will be : https://samples.openweathermap.org/ this one is working.
Upvotes: 1