问题
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¶m2=two' to my url, yet it works correctly in Postman. Here's the gist of my code:
final String url = "https://www.myurl.com/api/v1/test/";
String workingStringInPostman = "https://www.myurl.com/api/v1/test/123/?param1=one¶m2=two";
Map<String, String> qParams = {
'param1': 'one',
'param2': 'two',
};
var res = await http
.get(Uri.encodeFull("$url${widget.pk}/"),
headers: {HttpHeaders.authorizationHeader: "Token $token",
HttpHeaders.contentTypeHeader: "application/json"},
);
The ${widget.pk} is simply a integer value being pass (See the value 123 in the workingStringInPostman variable.
The qParams is there for connivence, in case a Uri parameter is needed.
A code example would be welcomed.
回答1:
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
回答2:
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);
回答3:
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¶m2=two";
to this: (Notice the '&' at the end).
String workingStringInPostman = "https://www.myurl.com/api/v1/test/123/?param1=one¶m2=two&";
回答4:
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.
来源:https://stackoverflow.com/questions/52824388/how-do-you-add-query-parameters-to-a-dart-http-request