How to retrieve raw post data from HttpServletRequest in java

前端 未结 3 510
栀梦
栀梦 2020-12-04 13:21

I\'m trying to get the post data in Java. Seems like it should be one of the simplest things to do right? I mean, HttpServletRequest.getParameter has to do it right? So how

相关标签:
3条回答
  • 2020-12-04 13:28

    The request body is available as byte stream by HttpServletRequest#getInputStream():

    InputStream body = request.getInputStream();
    // ...
    

    Or as character stream by HttpServletRequest#getReader():

    Reader body = request.getReader();
    // ...
    

    Note that you can read it only once. The client ain't going to resend the same request multiple times. Calling getParameter() and so on will implicitly also read it. If you need to break down parameters later on, you've got to store the body somewhere and process yourself.

    0 讨论(0)
  • 2020-12-04 13:37

    This worked for me: (notice that java 8 is required)

    String requestData = request.getReader().lines().collect(Collectors.joining());
    UserJsonParser u = gson.fromJson(requestData, UserJsonParser.class);
    

    UserJsonParse is a class that shows gson how to parse the json formant.

    class is like that:

    public class UserJsonParser {
    
        private String username;
        private String name;
        private String lastname;
        private String mail;
        private String pass1;
    //then put setters and getters
    }
    

    the json string that is parsed is like that:

    $jsonData: {    "username": "testuser",    "pass1": "clave1234" }
    

    The rest of values (mail, lastname, name) are set to null

    0 讨论(0)
  • 2020-12-04 13:51

    We had a situation where IE forced us to post as text/plain, so we had to manually parse the parameters using getReader. The servlet was being used for long polling, so when AsyncContext::dispatch was executed after a delay, it was literally reposting the request empty handed.

    So I just stored the post in the request when it first appeared by using HttpServletRequest::setAttribute. The getReader method empties the buffer, where getParameter empties the buffer too but stores the parameters automagically.

        String input = null;
    
        // we have to store the string, which can only be read one time, because when the
        // servlet awakens an AsyncContext, it reposts the request and returns here empty handed
        if ((input = (String) request.getAttribute("com.xp.input")) == null) {
            StringBuilder buffer = new StringBuilder();
            BufferedReader reader = request.getReader();
    
            String line;
            while((line = reader.readLine()) != null){
                buffer.append(line);
            }
            // reqBytes = buffer.toString().getBytes();
    
            input = buffer.toString();
            request.setAttribute("com.xp.input", input);
        }
    
        if (input == null) {
            response.setContentType("text/plain");
            PrintWriter out = response.getWriter();
            out.print("{\"act\":\"fail\",\"msg\":\"invalid\"}");
        }       
    
    0 讨论(0)
提交回复
热议问题