2017-12-16 4 views
0

feathers.js(https://feathersjs.com/)を使用してREST APIを開発しました。
パッケージを使用してFlutterでhttp 'read'リクエストを実行しようとすると、http/http.dartエラーが発生しました。 http.dartライブラリは、私がuriに渡すクエリパラメータを正しく解析できません。http urlパラメータ無効な文字エラー

Android Studioのデバッグコンソールでエラーが発生しました。

FormatException:E(文字84時)無効な文字/フラッター (11338):... &場所lk.com/weatherobs?last=true = [$で] Bellambi &場所=ナウラ[で$]。 ...

エラーは、角括弧とおそらく$記号( '[$ in]')が問題であることを示しています。

_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);  
} 

URLで、私は文字列の先頭に「r」を付けずに「r」を付けないようにしました。

私もフラッター/ダートでの経験の約3週間で人として角括弧例えば「[$で]」には成功とまったく同じエラー

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"}); 

でのparamsでのHttpClientを使用してみましたが

私はそれが基本的な問題だと信じていますが、数時間の研究が解決策を見つけ出していないものです。

uriクエリーパラメーターがどのように構造化されているか(角括弧[$ in])は、feathers.jsフレームワークによって決まります。

ご協力いただければ幸いです。

別のスレッドhttps://stackoverflow.com/questions/40568/are-square-brackets-permitted-in-urlsに私の注意に持って来られました:URL指定RFC 3986は、一般的に、URLに角括弧を許可していないこと

私の質問は、郵便配達員、Chromeブラウザ、およびaxios.jsを使用しているjavascriptアプリケーションで意図されたとおりに動作しますが、標準http.readメソッドを使用してFlutter/Dartで開発されたアプリケーションでは動作しません。

+1

https://stackoverflow.com/questions/40568/are-square-brackets-permitted-in-urlsが許可されていないと思われます。おそらく、https://api.dartlang.org/のクエリをエンコードすると動作します安定/ 1.24.3/dart-core/Uri/encodeQueryComponent.html –

+1

ようこそhttps://dartpad.dartlang.org/c8f79 14404fe02c407007d16e2463126 –

+0

すばらしい答えガンター。示されているように、角括弧を%5Bと%5Dに置き換えてください。ダーツパッドの例をありがとう。 –

答えて

2

URLには[]がサポートされていません(IPv6のホストIPを除く)。 Are square brackets permitted in URLs?を参照してください。

それらが同じようエンコードされたときにAPIがそれらを受け入れるかどうかを確認してください:

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

も参照してくださいapi.dartlang.org/stable/1.24.3/dart-core/Uri/...