I'm trying to receive a get request using Flutter and HttpClient.
This is complete code in what I'm trying to accomplish.
getSuggest() async {
try {
var httpClient = new HttpClient();
var uri = new Uri.http(
'http://autocomplete.geocoder.api.here.com', '/6.2/suggest.json', {
'app_id': 'APP_ID',
'app_code': 'APP_CODE',
'query': '123 Main Street',
'country': 'USA',
'language': 'en',
});
var request = await httpClient.getUrl(uri);
var response = await request.close();
var responseBody = await response.transform(Utf8Decoder()).join();
Map data = jsonDecode(responseBody);
print(data);
} catch (error) {
print(error);
}
}
And I'm using
import 'dart:io';
import 'dart:convert';
But my code always gets sent to the print(error)
and the error that gets printed is
FormatException: Invalid radix-10 number
Any ideas?
Thanks!!
The problem is the scheme. You don't have to set it in Uri.http
or Uri.https
methods, it is set automatically, so change with the following:
Uri.http(
'autocomplete.geocoder.api.here.com', '/6.2/suggest.json', {
'app_id': 'APP_ID',
'app_code': 'APP_CODE',
'query': '123 Main Street',
'country': 'USA',
'language': 'en',
});
And I suggest using http package and do something like that:
import 'package:http/http.dart' as http;
import 'dart:convert';
final json = const JsonCodec();
getSuggest() async {
try {
var uri = Uri.http(
'autocomplete.geocoder.api.here.com', '/6.2/suggest.json', {
'app_id': 'APP_ID',
'app_code': 'APP_CODE',
'query': '123 Main Street',
'country': 'USA',
'language': 'en',
});
var response = await http.get(uri);
var data = json.decode(response.body);
print(data);
} catch (error) {
print(error);
}
}
and use its http client
if you need to set much more things (e.g. User Agents
).