user5732979
user5732979

Reputation:

Reading Jetty Server Request body without making it null

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?

Upvotes: 1

Views: 1516

Answers (1)

Joakim Erdfelt
Joakim Erdfelt

Reputation: 49472

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.

Upvotes: 1

Related Questions