0

I am trying to send DELETE request with body. So, I decided to use CURL to complite this task.

curl -X DELETE "URL" -H "Authorization: Token XXX" -H "Content-Type: application/json" -d "ENTITY"

When I send above command from terminal, I am able to receive below output

{
    "data": null,
    "status_code": "1000",
    "status_message": "Success",
    "timestamp": "2023-03-23T13:43:20.910098300Z"
}

When I try to do same process with Spring boot app, It always fails. It returns empty string to test variable. When I debug, I can see that inputStream, process or processBuilder are not null

String command = "curl -X DELETE ........ ";
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
String test = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
statusMessage = new JSONObject(test).get("status_message").toString();

I have checked followings and I couldn't handle the problem;

  1. How to run a curl command from java?
  2. Java executing curl command not working on some commands
  3. How to use cURL in Java?
  4. cURL DELETE on java

BTW, I am not obligated to use cURL, if you have any other solutions, please let me know.

Serdar
  • 146
  • 8
  • You can’t just split the command by spaces it it’s that exact command. It will split in the middle of the arguments. Have you tried manually splitting it correctly into parts? – Sami Kuhmonen Jul 24 '23 at 09:56
  • Don't use split on the arguments to that command. It's asking for trouble. Go through the parameters carefully and form an array or `List` out of them. It's perfectly possible to do this in pure Java with the http client API – g00se Jul 24 '23 at 09:57
  • try using a java http client like the builtin RestTemplate instead of external cmdline tools... – Marc Stroebel Jul 24 '23 at 10:03
  • @SamiKuhmonen TBH, I haven't checked as you said but if I won't use split it returns "error=2, No such file or directory" error. – Serdar Jul 24 '23 at 10:12
  • @g00se can you give some examples if it's possible with http client, please? I have tried with that too but I couldn't send successful delete request with request body – Serdar Jul 24 '23 at 10:13
  • @MarcStroebel Could you give some examples, please? – Serdar Jul 24 '23 at 10:14
  • 1
    @Serdar https://www.baeldung.com/rest-template – Marc Stroebel Jul 24 '23 at 10:51
  • @Serdar As I said you have to set the arguments correctly. You need an array of arguments, manually separated. If you just do a split with space you get arguments like `Authorization:` and `Token` instead of `Authorization: Token...` – Sami Kuhmonen Jul 26 '23 at 12:33

1 Answers1

0

I suppose, there may be two problems. The first one can be in curl, to check it you can try another tool like Postman. The second one can be in Spring, and it seems more complicated than the first one. Look at this answer.

In my opinion, according to the specification, a payload in the DELETE method has no defined semantics, therefore another implementation of the HTTP standard may reject the DELETE withing a payload.

A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

If possible, it would be better not to use a payload within this HTTP method.

Maksim Eliseev
  • 487
  • 1
  • 11