How to send a JSONObject to a REST service?

前端 未结 3 1774
北海茫月
北海茫月 2021-01-06 13:33

Retrieving data from the REST Server works well, but if I want to post an object it doesn\'t work:

public static void postJSONObject(int store_type, Favorite         


        
相关标签:
3条回答
  • 2021-01-06 14:02

    I would go right to the server err_log or equivelant error log. The server knows why it rejected your request. If you don't have access, set up your own test server and duplicate the issue there so you can review the logs =)

    0 讨论(0)
  • Your C# is different than your Java, and not just in syntax.

    Your C# sends an application/json entity to the server via HTTP POST. I'll leave it up to HTTP purists as to whether that's appropriate use of POST (vs. PUT).

    Your Java creates a form, with a field of jsonString (whose value is the JSON), and sends an application/x-www-form-urlencoded entity to the server containing that form.

    0 讨论(0)
  • 2021-01-06 14:14

    Try setting the content type header:

    postMethod.addRequestHeader("Content-Type", "application/json");

    Btw, I strongly recommend Jersey. It has a REST client library which makes these kind of things much easier and more readable

    0 讨论(0)
提交回复
热议问题