kite_n_code
kite_n_code

Reputation: 438

http url parameter Invalid Character Error

I have developed a REST API using feathers.js (https://feathersjs.com/).
When trying to do a HTTP 'read' request in Flutter using package:http/http.dart I have encountered an error. The http.dart library is unable to correctly parse the query params I pass to the URI.

The error I receive through the Android Studio debug console is ;

FormatException: Invalid character (at character 84) E/flutter (11338): ...lk.com/weatherobs?last=true&location[$in]=Bellambi&location[$in]=Nowra ....

The error is indicating the square brackets and possibly the $ sign ('[$in]' ) are the issue.

_getDemoRequest() {
  String url = r"http://demoapi.ap-southeast-2.elasticbeanstalk.com/weatherobs?last=true&location[$in]=Bellambi&location[$in]=Nowra&location[$in]=Sydney Airport&location[$in]=Thredbo Top Station&location[$in]=Hobart&location[$in]=Coolangatta";
  http.read(url).then(print);    
}

In the URL I have tried prefixing the String with and without 'r' for a raw string to no avail.

I have also tried using httpClient with params with no success and the exact same error on the square brackets eg '[$in]'

  String httpbaseUri = "http://xxxx.ap-southeast-2.elasticbeanstalk.com";
  String qParams = r"?last=true&location[$in]=Bellambi&location[$in]=Nowra";
  String path = "/weatherobs";
  var _uri = new Uri.http(baseUri, path, qParams);
  await httpClient.read(_uri, headers: {"Accept": "application/json"});

As a person with approximately 3 weeks of Flutter/Dart experience I believe its an elementary problem, but one in which several hours of research has uncovered no solution.

The ways the URI query parameters are structured (with the square brackets ie [$in]) are dictated by the feathers.js framework.

Any help would be appreciated.

It has been brought to my attention in another thread https://stackoverflow.com/questions/40568/are-square-brackets-permitted-in-urls :

That the URL Specification RFC 3986 generally does not permit square brackets in an URL.

My question was triggered as the get request works as intended in Postman, Chrome Browser and also javascript applications using axios.js, but not in an application developed in Flutter/Dart using standard http.read methods.

Upvotes: 4

Views: 7111

Answers (2)

Dahkenangnon
Dahkenangnon

Reputation: 96

You can use this flutter package which allow you to communicate with your feathers js server from flutter app as said in: https://stackoverflow.com/a/65538226/12461921

Upvotes: 0

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657506

It doesn't look like [] are supported in the URL (except for the host IP for IPv6). See Are square brackets permitted in URLs?.

Please check if the API accepts them when they are encoded like:

void main() {
    var url = r'http://demoapi.ap-southeast-2.elasticbeanstalk.com/weatherobs';
    var locationKey = Uri.encodeQueryComponent(r'location[$in]');
    var qParams = 'last=true&$locationKey=Bellambi&$locationKey=Nowra&$locationKey=Sydney Airport&$locationKey=Thredbo Top Station&$locationKey=Hobart&$locationKey=Coolangatta'; 

    try {
      print(Uri.parse(url).replace(query: qParams));
    } catch(e) {
      print(e);
    }
}

DartPad example

See also api.dartlang.org/stable/1.24.3/dart-core/Uri/…

Upvotes: 4

Related Questions