HttpServletRequest get JSON POST data

0 votes
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.
Nov 29, 2018 in Java by Daisy
• 8,140 points
68,587 views

3 answers to this question.

0 votes

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

request.getParameter("cmd");

But only if the 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 your post data, as in your case when you post a 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 =  HTTP.toJSONObject(jb.toString());
  } catch (JSONException 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...
}
answered Nov 29, 2018 by Sushmita
• 6,920 points
if i put your code in a spring boot application, in a post controller, it will not work, it will fail at getReader() time
be correct with the imports
0 votes

request object from Python standard library. In the lastest requests package, you can use json parameter in requestspost() method to send a json dict, and the Content-Type in header will be set to application/json . There is no need to specify header explicitly.

answered Dec 16, 2020 by Gitika
• 65,770 points
0 votes

Configure the action as given below:

  1. Method: Since we need to post data, select 'POST' action from the dropdown list.
  2. URL: Enter the URL to which you wish to post data.
  3. Header: Enter 'Content-Type' as the header name.
  4. Value: Enter 'application/json' as the value for the specified header.
answered Dec 16, 2020 by Rajiv
• 8,870 points

Related Questions In Java

0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,480 points
3,826 views
+1 vote
1 answer

if i given invalid data how to get alert in selenium using java

Hello, Here is some of the method you ...READ MORE

answered Jul 20, 2020 in Java by Niroj
• 82,840 points
1,216 views
0 votes
0 answers

How to store and fetch nested json data from dynamodb in java

I want to maintain a book library. ...READ MORE

Sep 7, 2020 in Java by saswat
• 120 points

edited Sep 7, 2020 by Gitika 4,916 views
+17 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
253,587 views
0 votes
2 answers

How to convert a JSON String into Object in Java?

You could probably check out Google's Gson: ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
3,259 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,190 points
6,993 views
0 votes
1 answer

Difference between JSF, Servlet and JSP?

Servlet - it's java server side layer. JSP ...READ MORE

answered Jul 3, 2018 in Java by samarth295
• 2,220 points
1,536 views
0 votes
1 answer

Sending POST data in Android

public class CallAPI extends AsyncTask<String, String, String> ...READ MORE

answered Nov 28, 2018 in Java by Sushmita
• 6,920 points
1,575 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,907 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP