tsunade21
tsunade21

Reputation: 3462

Transform ServletInputStream to String

I am trying to get the body of a HttpServletRequest in a String. What's the best elegant way to do so?

Upvotes: 18

Views: 23959

Answers (2)

Aritz
Aritz

Reputation: 31659

Other way, using Guava:

ByteSource.wrap(ByteStreams.toByteArray(request.getInputStream()))
    .asCharSource(Charsets.UTF_8).read()

See also:

Upvotes: 2

siledh
siledh

Reputation: 3378

Using Apache Commons IO:

String requestStr = IOUtils.toString(request.getInputStream());

Upvotes: 19

Related Questions