{8.2.0-new} Response Body

The response body is accessed through the following method:

- {@link oajr.client2.RestResponse}
  - {@link oajr.client2.RestResponse#getBody() getBody()} returns {@link oajr.client2.RestResponseBody}

The {@link oajr.client2.RestResponseBody} class extends from the HttpClient {@link org.apache.http.HttpEntity} class and provides several convenience methods:

- {@link oajr.client2.RestResponseBody}
  - {@link oajr.client2.RestResponseBody#asInputStream() asInputStream()} returns InputStream
  - {@link oajr.client2.RestResponseBody#asReader() asReader()} returns Reader
  - {@link oajr.client2.RestResponseBody#asReader(Charset) asReader(Charset)} returns Reader
  - {@link oajr.client2.RestResponseBody#pipeTo(OutputStream) pipeTo(OutputStream)} returns {@link oajr.client2.RestResponse}
  - {@link oajr.client2.RestResponseBody#pipeTo(Writer) pipeTo(Writer)} returns {@link oajr.client2.RestResponse}
  - {@link oajr.client2.RestResponseBody#as(Type,Type...) as(Type,Type...)} returns T
  - {@link oajr.client2.RestResponseBody#as(Class) as(Class<T>)} returns T
  - {@link oajr.client2.RestResponseBody#asFuture(Class) asFuture(Class<T>)} returns Future<T>
  - {@link oajr.client2.RestResponseBody#asFuture(Type,Type...) asFuture(Type,Type...)} returns Future<T>
  - {@link oajr.client2.RestResponseBody#asString() asString()} returns String
  - {@link oajr.client2.RestResponseBody#asStringFuture() asStringFuture()} returns Future<String>
  - {@link oajr.client2.RestResponseBody#asAbbreviatedString(int) asAbbreviatedString(int)} returns String
  - {@link oajr.client2.RestResponseBody#asPojoRest(Class) asPojoRest(Class<?>)} returns {@link oaj.utils.PojoRest}
  - {@link oajr.client2.RestResponseBody#asPojoRest() asPojoRest()} returns {@link oaj.utils.PojoRest}
  - {@link oajr.client2.RestResponseBody#asMatcher(Pattern) asMatcher(Pattern)} returns {@link java.util.regex.Matcher}
  - {@link oajr.client2.RestResponseBody#asMatcher(String) asMatcher(String)} returns {@link java.util.regex.Matcher}

##### Examples:

// Parse into a bean. MyBean bean = client .get(URI) .run() .getBody().as(MyBean.class); // Parse into a linked-list of strings. List<String> l1 = client .get(URI) .run() .getBody().as(LinkedList.class, String.class); // Parse into a linked-list of beans. List<MyBean> l2 = client .get(URI) .run() .getBody().as(LinkedList.class, MyBean.class); // Parse into a linked-list of linked-lists of strings. List<List<String>> l3 = client .get(URI) .run() .getBody().as(LinkedList.class, LinkedList.class, String.class); // Parse into a map of string keys/values. Map<String,String> m4 = client .get(URI) .run() .getBody().as(TreeMap.class, String.class, String.class); // Parse into a map containing string keys and values of lists containing beans. Map<String,List<MyBean>> m5 = client .get(URI) .run() .getBody().as(TreeMap.class, String.class, List.class, MyBean.class);

The response body can only be consumed once unless it has been cached into memory. In many cases, the body is automatically cached when using the assertions methods or methods such as {@link oajr.client2.RestResponseBody#asString()}. However, methods that involve reading directly from the input stream cannot be called twice. In these cases, the {@link oajr.client2.RestResponse#cacheBody()} and {@link oajr.client2.RestResponseBody#cache()} methods are provided to cache the response body in memory so that you can perform several operations against it.

// Cache the response body so we can access it twice. InputStream inputStream = client .get(URI) .run() .cacheBody() .getBody().pipeTo(someOtherStream) .getBody().asInputStream();

Assertion methods are also provided for fluent-style calls:

- {@link oajr.client2.RestResponseBody}
  - {@link oajr.client2.RestResponseBody#assertString() assertString()} returns {@link oaj.assertions.FluentStringAssertion}
  - {@link oajr.client2.RestResponseBody#assertObject(Class) assertObject(Class<?>)} returns {@link oaj.assertions.FluentObjectAssertion}
  - {@link oajr.client2.RestResponseBody#assertBytes() assertBytes()} returns {@link oaj.assertions.FluentByteArrayAssertion}

##### Example:

// Assert that the body contains the string "Success". String body = client .get(URI) .run() .getBody().assertString().contains("Success") .getBody().asString();

Object assertions allow you to parse the response body into a POJO and then perform various tests on that resulting POJO.

##### Example:

// Parse bean into POJO and then validate that it was parsed correctly. MyBean bean = client.get(URI) .run() .getBody().assertObject(MyBean.class).json().is("{foo:'bar'}") .getBody().as(MyBean.class);