-3

I need to calculate the hash of my raw HTTP post request and compare it with the signature hash in the header of the same in Java. The problem is, I have tried multiple methods but I am unable to extract the exact POST request sent. Attached is the request:

enter image description here

I need exact same string (along with all the % and other symbols) since even a single space difference would generate an incorrect hash. Can we use custom-filters here, if yes then how exactly?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Sep 29 '17 at 11:03

1 Answers1

1

Are you using servlet or jersey? In servlet you can get the request's raw stream and convert to string. HttpServletRequest#getInputStream() or getReader.

e.g

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

Edit: Adding code that I have, which works.

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        InputStream is = request.getInputStream();
        //this convertStreamToString is my internal method. You can have any your own conversion API
        System.out.println("----"+convertStreamToString(is)); 
        processRequest(request, response);
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Optional
  • 4,387
  • 4
  • 27
  • 45
  • 1
    Caveat: When you do that, you might not be able to use `getParameter()` any more, so you'd have to process the content yourself too. As javadoc of [`getParameter()`](http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameter(java.lang.String)) says: *If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via `getInputStream()` or `getReader()` can interfere with the execution of this method.* – Andreas Sep 29 '17 at 06:26
  • That's a nice point. Thanks @Andreas. Can one get getParameters() first and then call the stream/reader? I need to revert back to API. – Optional Sep 29 '17 at 06:28
  • I think the problem is that if content is streamed (client decides that), then content can only be processed once. `getInputStream()`, `getReader()`, and `getParameter()` (and it's cousins) all *consume* the content. I think that if the content was sent in full, i.e. not streamed, they would all work, hence the "can interfere" in the javadoc. By streaming I mean that content is chunked, i.e. `content-length` header is not given. – Andreas Sep 29 '17 at 06:32
  • getReader() and getInputStream() does not help. They return null. However, I am getting values in getParamterMap(), but htis returns a map, but my problem is that I need exactly same raw data without any changes. – sanvi srivastava Sep 29 '17 at 06:36
  • Call this before calling any parameter. Like @Andreas pointed out the stream might have already been read and reset once you call getparameter(). – Optional Sep 29 '17 at 06:38
  • nopes..its still not working...even if only getReader() is called...The only hope I see here is getParameter family, but it doesnt return the exact data.. – sanvi srivastava Sep 29 '17 at 06:40
  • I am surprised as it works for me. I just had a servlet with post method and then I did `InputStream is = request.getInputStream();` and then `System.out.println("----"+convertStreamToString(is));` I got the exact full string. Off course convertStreamToString is my internal method. – Optional Sep 29 '17 at 06:46
  • You problem might be a `Filter` that calls `getParameter()`, which therefore consumes the content. In that case you have two options: 1) Remove the filter. 2) Put a new `Filter` first, which loads all content into memory, i.e. caching it, and then provides that content repeatedly. See [Http Servlet request lose params from POST body after read it once](https://stackoverflow.com/q/10210645/5221149) for how. – Andreas Sep 29 '17 at 06:50
  • Thanks! let me try..will update in sometime! – sanvi srivastava Sep 29 '17 at 07:00
  • It is working now..fynally! Thanks a lot for helpful suggestions! – sanvi srivastava Oct 06 '17 at 08:56