我正在制作post
内容类型为application/x-www-form-urlencoded
. 我无法在 post 方法中传递参数。下面是相关函数:
Future<ServerResponse> postAPICall(String apiName, Map<String, dynamic> params) async {
var url = Webservices.baseUrl + version + apiName;
var postUri = Uri.parse(url);
var completer = Completer<ServerResponse>();
HttpClient client = new HttpClient();
client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
HttpClientRequest request = await client.postUrl(postUri);
request.headers.set("content-type", "application/x-www-form-urlencoded");
request.headers.set("Authorization", Constant.authUser?.authToken == null
? ""
: Constant.authUser.authToken);
String jsonString = json.encode(params);
String paramName = 'param';
String formBody = paramName + '=' + Uri.encodeQueryComponent(jsonString);
List<int> bodyBytes = utf8.encode(formBody);
request.add(bodyBytes);
HttpClientResponse response = await request.close();
String data = await response.transform(utf8.decoder).join();
var jsValue = json.decode(data);
var serverResponseObj = ServerResponse.withJson(jsValue);
completer.complete(serverResponseObj);
return completer.future;
}