0

I am trying to send REST request by sending CURL command with Java Process Runtime with the code:

Process p = Runtime.getRuntime().exec("curl -i --user user_name:password -H \"Content-Type: application/json\" -X PUT url_without_quote -d @/home/user_name/put_content.json");  

However, I received 415 Unsupported Media Type even with header specified. I've tried the above command in terminal and it works fine with 200 return code. I've also tried GET request with the same options in curl command without -d content of PUT request, and it returns 200, and GET correct response content.

It's weird in the sense that the same command works in terminal but fails in java process running code.

Have you encountered the same issue or have any solution?

I doubt if there are some additional header need to be specified for PUT REST request or the character cases of "Content-Type: application/json" matters?

Ethan
  • 21
  • 3
  • http://stackoverflow.com/questions/22566433/http-415-unsupported-media-type-error-with-json – Steve Smith Apr 05 '17 at 13:53
  • The [man page](https://linux.die.net/man/1/curl) says that the URL has to be the last argument. Perhaps the `-d @filename` portion should occur before the URL. – VGR Apr 05 '17 at 14:36
  • 1
    @Ethan have you tried to use an array of arguments instead of a one-liner string as also suggested by this [answer](http://stackoverflow.com/questions/7134486/how-to-execute-command-with-parameters)? Next, why don't you send the request via Java directly? Invoking external processes might cause more problems [if not done properly](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) – Roman Vottner Apr 05 '17 at 14:45
  • @VGR I think the order of URL specification doesn't matter since after some trials, the original format is correct. – Ethan Apr 09 '17 at 05:43
  • @RomanVottner I've tried your suggestion, but the result shows the same 415 error. I've also tried invoke request via Java directly, but it seems to have conflict with other built-in rest service and thus not taking effect with both GET and PUT request. – Ethan Apr 09 '17 at 05:45
  • I've found the solution, please reference my answer! Thanks for all you comments. – Ethan Apr 09 '17 at 05:45
  • First of all you should not use curl command to make HTTP call instead you can use `HttpURLConnection` to do the same. – Sunil Kanzar Jun 17 '18 at 04:07

1 Answers1

2

After some trials, the following works:

Process p = Runtime.getRuntime().exec("curl -i --user user_name:password -H Content-Type:application/json -X PUT url_without_quote -d @/home/user_name/put_content.json");

From other thread of discussion, it suggests that the application should follow ':' character without any space character, and the double quotes are no need to be specified.

This works fine for me now! Thanks for the above comments!

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
Ethan
  • 21
  • 3