Getting request payload from POST request in Java servlet

Fasih Awan picture Fasih Awan · Jan 25, 2013 · Viewed 278.1k times · Source

I have a javascript library that is sending a POST request to my Java servlet, but in the doPost method, I can't seem to get the contents of the request payload. In chrome Developer Tools, all the content is in the Request Payload section in the headers tab, and the content is there, and I know that the POST is being received by the doPost method, but it just comes up blank.

For the HttpServletRequest object, what way can I get the data in the request payload?

Doing request.getParameter() or request.getAttributes() both end up with no data

Answer

davidfrancis picture davidfrancis · Jan 25, 2013

Simple answer:
Use getReader() to read the body of the request

More info:
There are two methods for reading the data in the body:

  1. getReader() returns a BufferedReader that will allow you to read the body of the request.

  2. getInputStream() returns a ServletInputStream if you need to read binary data.

Note from the docs: "[Either method] may be called to read the body, not both."