0

I have seen documentation around it only shows how to delete using url but it doesn't show how to pass in parameters to delete only that specific item in the json array?

Calling the delete function:

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

Parameters that need to be passed in are:

  1. String user_id
  2. String price

JSON array:

{
    "user_id": "user2",
    "price": "$34"
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
irongirl
  • 895
  • 7
  • 14
  • 31

2 Answers2

1

Found out the answer using HTTP client request when using delete on body method!

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;
  }
irongirl
  • 895
  • 7
  • 14
  • 31
0

I recently had the same problem and the only way to Solve it was to use http client :

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();
    }
  }