How do you add query parameters to a Dart http request?

前端 未结 6 1822
广开言路
广开言路 2020-12-05 22:48

How do you correctly add query parameters to a Dart http get request? I been unable to get my request to respond correctly when trying to append the \'?param1=one¶m

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 23:25

    If you dont want to override the scheme of base endpoint url, use the below technique to convert the map to query string and append it to the base endpoint url

    var endpointUrl = 'https://www.myurl.com/api/v1/user';
    Map queryParams = {
      'param1': '1',
      'param2': '2'
    };
    String queryString = Uri(queryParameters: queryParams).query;
    
    var requestUrl = endpointUrl + '?' + queryString; // result - https://www.myurl.com/api/v1/user?param1=1¶m2=2
    var response = await http.get(requestUrl);
    

提交回复
热议问题