views:

58

answers:

4

I am HTTP POST-ing to URL http://laptop:8080/apollo/services/rpc?cmd=execute

with POST data

{ "jsondata" : "data" }

Http request has Content-Type of application/json; charset=UTF-8

How do I get the POST data (jsondata) from HttpServletRequest?

If I enumerate the request params, I can only see one param, which is "cmd", not the POST data.

+2  A: 

Try this:

http://stackoverflow.com/questions/1548782/retrieving-json-object-literal-from-httpservletrequest

Steve Perkins
@Steve Thanks for the link, I should search stackoverflow before posting a new question.
portoalet
A: 

Are you posting from a different source (so different port, or hostname)? If so, this very very recent topic I just answered might be helpful.

The problem was the XHR Cross Domain Policy, and a useful tip on how to get around it by using a technique called JSONP. The big downside is that JSONP does not support POST requests.

I know in the original post there is no mention of JavaScript, however JSON is usually used for JavaScript so that's why I jumped to that conclusion

CharlesLeaf
+1  A: 

Normaly you can GET and POST parameters in a servlet the same way:

request.getParameter("cmd");

But only if they POST data is encoded as key-value pairs of content type: "application/x-www-form-urlencoded" like when you use a standard HTML form.

If you use a different encoding schema for you're post data, as in you're case when you post an json data stream, you need to use a custom decoder that can process the raw datastream from:

BufferedReader reader = request.getReader();

Json post processing example (uses org.json package )

public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

  StringBuffer jb = new StringBuffer();
  String line = null;
  try {
    BufferedReader reader = request.getReader();
    while ((line = reader.readLine()) != null)
      jb.append(line);
  } catch (Exception e) { //report an error }

  try {
    JSONObject jsonObject = new JSONObject(jb.toString());
  } catch (ParseException e) {
    // crash and burn
    throw new IOException("Error parsing JSON request string");
  }

  // Work with the data using methods like...
  // int someInt = jsonObject.getInt("intParamName");
  // String someString = jsonObject.getString("stringParamName");
  // JSONObject nestedObj = jsonObject.getJSONObject("nestedObjName");
  // JSONArray arr = jsonObject.getJSONArray("arrayParamName");
  // etc...
}

For more information on JSON Java libraries you might want to check out: http://stackoverflow.com/questions/338586/a-better-java-json-library

Kdeveloper
A: 

First, you need to set a parameter in HTTP Post, e.g. (let's say mine is "jsonValue") and tie it to the JSON object value (in String form).

and on HttpServletRequest (in doPost) do the following:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String jsonValue = request.getParameter("jsonValue");

    if (jsonValue != null && !jsonValue.isEmpty()) {
        JSONObject object = new JSONObject(jsonValue);

        //do whatever you need to do with JSON....

    }
}
The Elite Gentleman