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

前端 未结 6 1815
广开言路
广开言路 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:23

    Use Uri constructor to build your query, it has a queryParameter property.

    var uri = Uri(
      scheme: 'https',
      host: 'example.com',
      path: '/foo/bar',
      fragment: 'baz',
      queryParameters: _yourQueryParameters,
    );
    
    var response = await http.get(uri);
    if (response.statusCode == 200) {
      var json = jsonDecode(response.body);
      // Do whatever you want to do with json. 
    }
    
    0 讨论(0)
  • 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<String, String> 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&param2=2
    var response = await http.get(requestUrl);
    
    0 讨论(0)
  • 2020-12-05 23:26

    There is a dart package that provides some helper classes for http requests.

    BasicUtils : https://github.com/Ephenodrom/Dart-Basic-Utils

    Install it with:

    dependencies:
      basic_utils: ^1.4.0
    

    Usage

    You can add a map of headers and query parameters to each request. See the example :

    // Define some headers and query parameters
    Map<String, String> headers = {
      "Accept": "application/json"
    };
    Map<String, String> queryParameters = {
      "foo": "bar"
    };
    
    // Body
    String body = "{ 'some':'json'}";
    
    // Send request
    Map<String, dynamic> responseData = await HttpUtils.postForJson("api.com/dosomething", body,
          headers: headers, queryParameters: queryParameters);
    

    Additional information :

    These are all methods from the HttpUtils class.

    Future<Map<Response> getForFullResponse(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
    Future<Map<String, dynamic>> getForJson(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
    Future<String> getForString(String url,{Map<String, dynamic> queryParameters,Map<String, String> headers});
    Future<Map<Response> postForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<Map<String, dynamic>> postForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<String> postForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<Response> putForFullResponse(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<Map<String, dynamic>> putForJson(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<String> putForString(String url, String body,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<Response deleteForFullResponse(String url,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<Map<String, dynamic>> deleteForJson(String url,{Map<String, String> queryParameters,Map<String, String> headers});
    Future<String> deleteForString(String url,{Map<String, String> queryParameters,Map<String, String> headers});
    Map<String, dynamic> getQueryParameterFromUrl(String url);
    String addQueryParameterToUrl(String url, Map<String, dynamic> queryParameters);
    
    0 讨论(0)
  • 2020-12-05 23:26

    Got the same question. The accepted answer won't work if my url is localhost with port like https://localhost:5001. After spending 1 day to search for solution, I come up with Dio library. Following is my solution using Dio:

    var _dio = new Dio();
    var options = new Options;
    options.headers['Authorization'] = 'bearer $token';
    options.contentType = 'application/json';
    String url = "https://www.myurl.com";
    Map<String, String> qParams = {
      'param1': 'one',
      'param2': 'two',
    };
    
    var res = await _dio.get(url, options: options, queryParameters: qParams);
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-05 23:39

    You'll want to construct a Uri and use that for the request. Something like

    var queryParameters = {
      'param1': 'one',
      'param2': 'two',
    };
    var uri =
        Uri.https('www.myurl.com', '/api/v1/test/${widget.pk}', queryParameters);
    var response = await http.get(uri, headers: {
      HttpHeaders.authorizationHeader: 'Token $token',
      HttpHeaders.contentTypeHeader: 'application/json',
    });
    

    See https://api.dartlang.org/stable/2.0.0/dart-core/Uri/Uri.https.html

    0 讨论(0)
  • 2020-12-05 23:41

    The accepted answer didn't work for me but adding a '&' without quotes to end of the URL solves my problem. In this case, change the following line:

    String workingStringInPostman = "https://www.myurl.com/api/v1/test/123/?param1=one&param2=two";
    

    to this: (Notice the '&' at the end).

    String workingStringInPostman = "https://www.myurl.com/api/v1/test/123/?param1=one&param2=two&";
    
    0 讨论(0)
提交回复
热议问题