1

I have a Jetty.Server.Request object, which is an HTTP request whose body I need to use in multiple methods.

I access the body's contents like so -

String contents = baseRequest.getReader().readLine();

However, this seems to remove the body from the HTTP request. If I then try to access it again like so -

String contents2 = baseRequest.getReader().readLine();

contents2 will be null.

How can I read the body of the request object without affecting the request?

  • Possible duplicate of [Http Servlet request lose params from POST body after read it once](http://stackoverflow.com/questions/10210645/http-servlet-request-lose-params-from-post-body-after-read-it-once) – Joakim Erdfelt May 09 '16 at 21:20

1 Answers1

1

Per the Servlet spec, the stream is only available once.

Make a copy of it yourself (either to memory, or to disk for later reading)

This is by design, as many request bodies can by quite large and there simply wouldn't be enough memory to handle rereads in a sane way.

Be sure you check out the prior answers for this:

Http Servlet request lose params from POST body after read it once

As those answer demonstrate a few ways to accomplish multiple reads of the same request body.

Community
  • 1
  • 1
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Zut! Appreciate the information. I suppose I'll write it to a file. Thank you! –  May 09 '16 at 21:24
  • Can you link the documentation for me? This might be useful for other questions I have. –  May 09 '16 at 21:27