Response Status
After execution using RestRequest.run() or RestRequest.complete(), the following methods can be used to get the response status:
int
String
Example
// Only interested in status code.
int statusCode = client.get(URI).complete().getStatusCode();
Equivalent methods with mutable parameters are provided to allow access to status values without breaking fluent call chains.
Example
// Interested in multiple values.
Value statusCode = Value.empty();
Value reasonPhrase = Value.empty();
client.get(URI).complete().getStatusCode(statusCode).getReasonPhrase(reasonPhrase);
System.err.println("statusCode="+statusCode.get()+", reasonPhrase="+reasonPhrase.get());
note
If you are only interested in the response status and not the response body, be sure to use RestRequest.complete() instead of RestRequest.run() to make sure the response body gets automatically cleaned up. Otherwise you must consume the response yourself.
The assertion method is provided for quickly asserting status codes in fluent calls.
Example
// Status assertion using a static value.
String content1 = client.get(URI)
.run()
.assertStatus().asCode().isBetween(200,399)
.getContent().asString();
// Status assertion using a predicate.
String content2 = client.get(URI)
.run()
.assertStatus().asCode().is(x -> x<400)
.getContent().asString();