How can I pull the body out of a POST-запроса ?

I tried using getReader

 private Map<String, Object> getBodyFromPostRequest(HttpServletRequest request) throws IOException { Map<String, Object> bodyParameters = new HashMap<>(); Object[] body = request.getReader().lines().toArray(); ... } 

but in the end, nothing appears in the body array.

forming a request on the client:

 function signUpAction(){ var xhr; if (window.XMLHttpRequest) { // Mozilla, Safari, ... xhr = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE xhr = new ActiveXObject("Microsoft.XMLHTTP"); } var log = document.getElementById('login').value; var pas = document.getElementById('password').value; xhr.open('POST', '/reg', true); var body = 'login=' + encodeURIComponent(log) + '&password=' + encodeURIComponent(pas); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function() { if (xhr.status != 200) { alert( xhr.status + ': ' + xhr.statusText ); } else { console.log( xhr.responseText ); } }; xhr.send(body); } 

    3 answers 3

    If the parameters were sent by POST as application/x-www-form-urlencoded , they should be available as follows:

     Map<String, String[]> request.getParameterMap(); 

    The key in Map-e is the name of the parameter, the value is an array of corresponding values. There will also be query-parameters.

    • That is, the request parameters from the body must also be available through this method? Now I tried, but in the getParameterMap() method getParameterMap() only the parameters explicitly passed in the request-URI are I. Smirnov
    • And request.getReader (). Lines () at least returns something? maybe your data from the client does not leave at all? - Nofate
    • java.util.stream.ReferencePipeline$Head@1e9cee - well, an object, then returns. The question of what to do with it, apparently, is worth seeing the docks. And at the expense of data from the client, because the parameters from the request-URI can be calculated, or is it not what you mean? - I. Smirnov
    • Found what the error was. If earlier somewhere there was an appeal to the methods of getting parameters, then the body becomes empty. Found here . - I. Smirnov
    • But by the way, using Map<String, String[]> request.getParameterMap(); and try to pull out the data using the login or password key, we end up with null - I. Smirnov

    Or you can use the Guava library:

     String test = CharStreams.toString(request.getReader()); 

    Or, if Java 8, you can use the following example.

     String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); 

    The main thing to consider is that if previously somewhere there was an appeal to the methods for obtaining parameters, the body becomes empty. Found here

      Or you can

       InputStream in = null; try { in = request.getInputStream(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
      • Please try to write more detailed answers. I am sure the author of the question would be grateful for your expert commentary on the code above. - Nicolas Chabanovsky
      • @Nicolas Chabanovsky, I see no reason to duplicate what was written above. The given code is trivial and taken by me from the combat payment system. - plesser