2017-12-16 1 views
0

나는 feathers.js (https://feathersjs.com/)를 사용하여 REST API를 개발했습니다.
패키지를 사용하여 Flutter에서 http 'read'요청을하려고하면 : http/http.dart 오류가 발생했습니다. http.dart 라이브러리가 uri에 전달한 쿼리 매개 변수를 올바르게 구문 분석 할 수 없습니다.HTTP URL 매개 변수 잘못된 문자 오류

나는 안드로이드 스튜디오 디버깅 콘솔을 통해 나타나는 오류입니다;

FormatException을 : E (84 문자) 잘못된 문자/조동 (11,338) ... & 위치 lk.com/weatherobs?last=true = $ 있음] Bellambi = & 위치 나우 [$에서]. ...

오류는 대괄호와 가능성이 $ 기호 ('[$에서]') 문제가 있습니다를 표시합니다.

_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와 세션 객체를 사용하여 시도했다

나는 그것의 초등 문제를 믿는다. 그러나 몇 시간의 연구가 아무런 해결책도 발견하지 못했다.

feathers.js 프레임 워크에 의해 결정된다 (대괄호, 즉 [에 $] 포함) URI 쿼리 매개 변수가 구성되는 방법.

도움을 주시면 감사하겠습니다.

다른 스레드 https://stackoverflow.com/questions/40568/are-square-brackets-permitted-in-urls 내 관심을 끌게되었습니다 URL 스펙 RFC 3986는 일반적으로 URL에 대괄호를 허용하지 않습니다

있다.

get 요청이 Postman, Chrome 브라우저 및 자바 스크립트 응용 프로그램 (axios.js)을 사용하여 작동하지만 Flutter/Dart에서 표준 http.read 메소드를 사용하여 개발 한 응용 프로그램에서는 작동하지 않으므로 내 질문이 실행되었습니다. 그것은 []처럼 보이지 않는

+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

최고 답변 Gunter. 표시된대로 대괄호를 % 5B 및 % 5D로 교체하십시오. 다트 패드 예제를 가져 주셔서 감사합니다. –

답변

2

은 (호스트 IPv6의 IP 제외) URL에서 지원됩니다. 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/...