有一个 dart 包,它为 http 请求提供了一些帮助类。它支持向发布请求添加标头。
Github:https ://github.com/Ephenodrom/Dart-Basic-Utils
安装它:
dependencies:
basic_utils: ^1.5.1
用法
Map<String, String> headers = {
"content-type": "application/json"
};
Map<String, String> queryParameters = {
"Some": "Parameter"
};
String url = "";
String payloadAsString = "{\"foo\":\"bar\"}";
Map<String, dynamic> body;
try {
body = await HttpUtils.postForJson(url, payloadAsString,
queryParameters: queryParameters, headers: headers);
} catch (e) {
// Handle exception, for example if response status code != 200-299
}
// do something with the response body
print(body);
附加信息 :
这些都是来自 HttpUtils 类的方法。
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);