Mangue  Sutcliff
Mangue Sutcliff

Reputation: 1619

JAVA Delete API with Array String Body

Sorry in advance for my googled english, I work with an API and I make a JAVA software that allows to use it. I need to make a DELETE and the software. I have to perform a deletion, and with the supplied software to test the API, I am shown that I have to add the line in a body to remove it, like this : ["email","Termine","13/03/2018 09:52:20",etc...,""]. The body must contain a String Array with all the contents of the line to delete. I can make it work in the test software.

However I can not understand how to make a DELETE with JAVA. I can make it work in the software test. That's what I did for now:

 public static  String delete(String json, String nomUrl) throws IOException {
    URL url = new URL(baseUrl + "survey/"+ nomUrl + "/data");
    //String json = "[\"[email protected]\",\"Contacte\",\"10/04/2018 11:30:05\",\"Avoriaz\",\"Office de Tourisme\",\"Accueil OT\",\"Neerlandais\",\"Semaine 6\",\"Periode 2\",\"16\",\"\",\"Hiver 2018\",\"BJBR-CDQB\",\"04/12/2018 14:15:13\",\"04/12/2018 14:15:13\",\"04/12/2018 14:15:13\",\"\",\"Direct\",\"\",\"\",\"\"]\n";
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("DELETE");
    con.setRequestProperty("Content-Type","application/json");
    con.setRequestProperty("Accept","application/json");
    con.setRequestProperty("Authorization","Bearer "+token);

    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(json);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    StringBuilder responce = new StringBuilder();
    responce.append("\\nSending 'DELETE' request to URL : ").append(url);
    responce.append("\nResponse Code : ").append(responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;

    while ((inputLine = in.readLine()) != null) {
        responce.append("\n").append(inputLine);
    }
    in.close();

    return responce.toString();
}

I was inspired by what I did for the post and the get. But I do not see how to add a body correctly with my String Array to my delete function because it doesn't work, and the internet did not help me ... Thank you in advance for your help !

EDIT : Finally, my code works. So if you want to delete with body, you can use this code. However, the problem comes from the json: I'm french, so was some accents on my words and special characters. After cleaning my string, everythings works.

Upvotes: 1

Views: 335

Answers (2)

Mangue  Sutcliff
Mangue Sutcliff

Reputation: 1619

EDIT : Finally, my code works. So if you want to delete with body, you can use this code. However, the problem comes from the json: I'm french, so was some accents on my words and special characters. After cleaning my string, everythings works.

Upvotes: 1

Jay Dangar
Jay Dangar

Reputation: 3479

You can create a POJO class with the fields required by RequestBody and send it to API, by Serializing the Object (Serialization means converting Java Objects into JSON and this can be done via GSON library). on API side you can easily get the ArrayList or whatever you want, just need to create same POJO class on server side as well, RequestBody will deserialize this JSON into Appropriate class, now via object of the class you can get whatever variables you want. Hope this helps.

Upvotes: 0

Related Questions