0

postのコンテンツ タイプで API 呼び出し関数を作成しています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;
}
4

2 に答える 2