传递参数以删除-Flutter

时间:2019-03-06 06:34:35

标签: json flutter http-delete

我看过有关它的文档仅显示了如何使用url删除,但是没有显示如何传递参数以仅删除json数组中的特定项?

调用删除功能:

  main() {
    HttpClient httpClient = new HttpClient();
    httpClient.delete( '35.186.145.243', 8080, '/users');
    httpClient.close();
    return "Success";
  }

需要传递的参数是:

  1. 字符串user_id
  2. 字符串price

JSON数组:

{
    "user_id": "user2",
    "price": "$34"
}

2 个答案:

答案 0 :(得分:0)

使用主体上的删除方法时使用HTTP客户端请求找出答案!

main() async {
    String url = "http://35.186.145.243:8080/users";

    Map map = {
      'price': '$34',
      'user_id': 'user2'
    };

    print(await apiRequest(url, map));
  }

  Future<String> apiRequest(String url, Map jsonMap) async {
    HttpClient httpClient = new HttpClient();
    HttpClientRequest request = await httpClient.deleteUrl(Uri.parse(url));
    request.headers.set('content-type', 'application/json');
    request.add(utf8.encode(json.encode(jsonMap)));

    HttpClientResponse response = await request.close();
    String statusCode = response.statusCode.toString();
    String reply = await response.transform(utf8.decoder).join();
    responseMessage = statusCode;

    print(statusCode);

    httpClient.close();
    return reply;
  }

答案 1 :(得分:0)

我最近遇到了同样的问题,解决此问题的唯一方法是使用http客户端:

Future<String> apiRequest() async {
    Map<String, dynamic> Object;
      Object= {
             'price': '$34',
             'user_id': 'user2'
              };
    final client = http.Client();
    try {
      final response = await client.send(http.Request("DELETE", Uri.parse("35.186.145.243:8080"))
        ..headers["Content-Type"] = "application/json"
        ..body = json.encode(Object));
    } catch (e) {
      print(e);
    } finally {
      client.close();
    }
  }