I have a multiword String that I'd like to convert to a GET request parameter.
I have an API endpoint /search
that takes in the parameter query
. Now typically your request would look like http://host/search?query=Hello+World
.
I have a String
Hello World that I'd like to convert to this URL encoded parameter.
Ofcourse, I could just write the logic to break it into words and add a +
in between but I was wondering if the URI class could help with this
I'm using Dart's httpClient
to make a request.
Future<String> _getJsonData(String queryToSearch) async {
List data = new List();
var httpClient = new HttpClient();
var request = await httpClient.getUrl(Uri.parse(
config['API_ENDPOINT'] + '/search?query=' +
queryToSearch));
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
var jsonString = await response.transform(utf8.decoder).join();
data = json.decode(jsonString);
print(data[0]);
return data[0].toString();
} else {
return "{}";
}
}
Essentially, need to encode queryToSearch
as the URL parameter.
Uri.ecode
but that's for encoding the POST body. The documentation wasn't very explicit about passing queries like this in GET requests – Clementina