Class RestResponse
- All Implemented Interfaces:
- AutoCloseable,- HttpMessage,- HttpResponse
- Direct Known Subclasses:
- MockRestResponse
 Instances of this class are created by calling the RestRequest.run() method.
 
Example:
   
 Alternatively, you can rely on RestRequest.close() to automatically close the response:
 
   
Notes:
- This class implements AutoCloseableand can be used in try-with-resources blocks. Theclose()method allows unchecked exceptions to propagate for debuggability, while catching and logging checked exceptions to follow AutoCloseable best practices.
See Also:
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedRestResponse(RestClient client, RestRequest request, HttpResponse response, Parser parser) Constructor.
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdds a header to this message.voidAdds a header to this message.Provides the ability to perform fluent-style assertions on the response character encoding.Provides the ability to perform fluent-style assertions on this response body.assertContent(String value) Provides the ability to perform fluent-style assertions on this response body.assertContentMatches(String value) Provides the ability to perform fluent-style assertions on this response body.assertHeader(String name) Provides the ability to perform fluent-style assertions on a response header.Provides the ability to perform fluent-style assertions on the responseStatusLineobject.assertStatus(int value) Provides the ability to perform fluent-style assertions on the response status code.Caches the response body so that it can be read as a stream multiple times.voidclose()Closes this response.consume()Consumes the response body.booleancontainsHeader(String name) Checks if a certain header is present in this message.Returns all the headers of this message.Shortcut for retrieving the response charset from theContent-Type header.Returns the body of the response.Shortcut for retrieving the response content type from theContent-Type header.Obtains the message entity of this response.getFirstHeader(String name) Returns the first header with a specified name of this message.Returns the response header with the specified name.getHeaders(String name) Returns all the headers with a specified name of this message.getLastHeader(String name) Returns the last header with a specified name of this message.Obtains the locale of this response.Deprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .protected HttpPartParserSessionCreates a session of the client-default parat parser.protected HttpPartParserSessiongetPartParserSession(HttpPartParser parser) Creates a session of the specified part parser.Returns the protocol version this message is compatible with.Returns the status line reason phrase of the response.Returns the request object that created this response object.intReturns the status code of the response.Obtains the status line of this response.getStringHeader(String name) Shortcut for callinggetHeader(name).asString().Returns an iterator of all the headers.headerIterator(String name) Returns an iterator of the headers with a given name.Logs a message.Logs a message.voidremoveHeader(Header header) Removes a header from this message.voidremoveHeaders(String name) Removes all headers with a certain name from this message.voidsetEntity(HttpEntity entity) Associates a response entity with this response.voidOverwrites the first header with the same name.voidOverwrites the first header with the same name.voidsetHeaders(Header[] headers) Overwrites all the headers in the message.voidChanges the locale of this response.voidsetParams(HttpParams params) Deprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .voidsetReasonPhrase(String reason) Updates the status line of this response with a new reason phrase.voidsetStatusCode(int code) Updates the status line of this response with a new status code.voidsetStatusLine(ProtocolVersion ver, int code) Sets the status line of this response.voidsetStatusLine(ProtocolVersion ver, int code, String reason) Sets the status line of this response with a reason phrase.voidsetStatusLine(StatusLine statusline) Sets the status line of this response.
- 
Constructor Details- 
RestResponseprotected RestResponse(RestClient client, RestRequest request, HttpResponse response, Parser parser) Constructor.- Parameters:
- client- The RestClient that created this response.
- request- The REST request.
- response- The HTTP response. Can be- null .
- parser- The overridden parser passed into- RestRequest.parser(Parser).
 
 
- 
- 
Method Details- 
getRequestReturns the request object that created this response object.- Returns:
- The request object that created this response object.
 
- 
consumeConsumes the response body.This is equivalent to closing the input stream. Any exceptions thrown during close are logged but not propagated. - Returns:
- This object.
 
- 
getStatusCodeReturns the status code of the response. Shortcut for callinggetStatusLine().getStatusCode().- Returns:
- The status code of the response.
 
- 
getReasonPhraseReturns the status line reason phrase of the response. Shortcut for callinggetStatusLine().getReasonPhrase().- Returns:
- The status line reason phrase of the response.
 
- 
assertStatusProvides the ability to perform fluent-style assertions on the responseStatusLineobject.Examples:MyBean bean =client .get(URI ) .run() .assertStatus().asCode().is(200) .getContent().as(MyBean.class );- Returns:
- A new fluent assertion object.
 
- 
assertStatusProvides the ability to perform fluent-style assertions on the response status code.Examples:MyBean bean =client .get(URI ) .run() .assertStatus(200) .getContent().as(MyBean.class );- Parameters:
- value- The value to assert.
- Returns:
- A new fluent assertion object.
 
- 
getStringHeaderShortcut for callinggetHeader(name).asString().- Parameters:
- name- The header name.
- Returns:
- The header value, never null 
 
- 
getCharacterEncodingShortcut for retrieving the response charset from theContent-Type header.- Returns:
- The response charset.
- Throws:
- RestCallException- If REST call failed.
 
- 
getContentTypeShortcut for retrieving the response content type from theContent-Type header.This is equivalent to calling getHeader( ."Content-Type" ).as(ContentType.class )- Returns:
- The response charset.
- Throws:
- RestCallException- If REST call failed.
 
- 
assertCharsetProvides the ability to perform fluent-style assertions on the response character encoding.Examples:// Validates that the response content charset is UTF-8. client .get(URI ) .run() .assertCharset().is("utf-8" );- Returns:
- A new fluent assertion object.
- Throws:
- RestCallException- If REST call failed.
 
- 
assertHeaderProvides the ability to perform fluent-style assertions on a response header.Examples:// Validates the content type header is provided. client .get(URI ) .run() .assertHeader("Content-Type" ).exists();// Validates the content type is JSON. client .get(URI ) .run() .assertHeader("Content-Type" ).is("application/json" );// Validates the content type is JSON using test predicate. client .get(URI ) .run() .assertHeader("Content-Type" ).is(x ->x .equals("application/json" ));// Validates the content type is JSON by just checking for substring. client .get(URI ) .run() .assertHeader("Content-Type" ).contains("json" );// Validates the content type is JSON using regular expression. client .get(URI ) .run() .assertHeader("Content-Type" ).isPattern(".*json.*" );// Validates the content type is JSON using case-insensitive regular expression. client .get(URI ) .run() .assertHeader("Content-Type" ).isPattern(".*json.*" ,CASE_INSENSITIVE );The assertion test returns the original response object allowing you to chain multiple requests like so: // Validates the header and converts it to a bean. MediaTypemediaType =client .get(URI ) .run() .assertHeader("Content-Type" ).isNotEmpty() .assertHeader("Content-Type" ).isPattern(".*json.*" ) .getHeader("Content-Type" ).as(MediaType.class );- Parameters:
- name- The header name.
- Returns:
- A new fluent assertion object.
 
- 
getContentReturns the body of the response. This method can be called multiple times returning the same response body each time.- Returns:
- The body of the response.
 
- 
assertContentProvides the ability to perform fluent-style assertions on this response body.Examples:// Validates the response body equals the text "OK". client .get(URI ) .run() .assertContent().is("OK" );// Validates the response body contains the text "OK". client .get(URI ) .run() .assertContent().isContains("OK" );// Validates the response body passes a predicate test. client .get(URI ) .run() .assertContent().is(x ->x .contains("OK" ));// Validates the response body matches a regular expression. client .get(URI ) .run() .assertContent().isPattern(".*OK.*" );// Validates the response body matches a regular expression using regex flags. client .get(URI ) .run() .assertContent().isPattern(".*OK.*" ,MULTILINE &CASE_INSENSITIVE );// Validates the response body matches a regular expression in the form of an existing Pattern. Patternpattern = Pattern.compile (".*OK.*" );client .get(URI ) .run() .assertContent().isPattern(pattern );The assertion test returns the original response object allowing you to chain multiple requests like so: // Validates the response body matches a regular expression. MyBeanbean =client .get(URI ) .run() .assertContent().isPattern(".*OK.*" ); .assertContent().isNotPattern(".*ERROR.*" ) .getContent().as(MyBean.class );Notes:- 
      If no charset was found on the Content-Typeresponse header,"UTF-8" is assumed.
- 
      When using this method, the body is automatically cached by calling the ResponseContent.cache().
- The input stream is automatically closed after this call.
 - Returns:
- A new fluent assertion object.
 
- 
      If no charset was found on the 
- 
assertContentProvides the ability to perform fluent-style assertions on this response body.A shortcut for calling assertContent().is( .value )Examples:// Validates the response body equals the text "OK". client .get(URI ) .run() .assertContent("OK" );- Parameters:
- value- The value to assert.
- Returns:
- This object.
 
- 
assertContentMatchesProvides the ability to perform fluent-style assertions on this response body.A shortcut for calling assertContent().asString().isMatches( .value )- Parameters:
- value- The value to assert.
- Returns:
- This object.
- See Also:
 
- 
cacheContentCaches the response body so that it can be read as a stream multiple times. This is equivalent to calling the following:getContent().cache(); - Returns:
- The body of the response.
 
- 
logLogs a message.- Parameters:
- level- The log level.
- t- The throwable cause.
- msg- The message with- MessageFormat-style arguments.
- args- The arguments.
- Returns:
- This object.
 
- 
logLogs a message.- Parameters:
- level- The log level.
- msg- The message with- MessageFormat-style arguments.
- args- The arguments.
- Returns:
- This object.
 
- 
getStatusLineObtains the status line of this response. The status line can be set using one of the setStatusLine methods, or it can be initialized in a constructor.- Specified by:
- getStatusLinein interface- HttpResponse
- Returns:
- The status line, or null if not yet set.
 
- 
setStatusLineSets the status line of this response.- Specified by:
- setStatusLinein interface- HttpResponse
- Parameters:
- statusline- The status line of this response
 
- 
setStatusLineSets the status line of this response.The reason phrase will be determined based on the current locale. - Specified by:
- setStatusLinein interface- HttpResponse
- Parameters:
- ver- The HTTP version.
- code- The status code.
 
- 
setStatusLineSets the status line of this response with a reason phrase.- Specified by:
- setStatusLinein interface- HttpResponse
- Parameters:
- ver- The HTTP version.
- code- The status code.
- reason- The reason phrase, or- null to omit.
 
- 
setStatusCodeUpdates the status line of this response with a new status code.- Specified by:
- setStatusCodein interface- HttpResponse
- Parameters:
- code- The HTTP status code.
- Throws:
- IllegalStateException- If the status line has not be set.
 
- 
setReasonPhraseUpdates the status line of this response with a new reason phrase.- Specified by:
- setReasonPhrasein interface- HttpResponse
- Parameters:
- reason- The new reason phrase as a single-line string, or- null to unset the reason phrase.
- Throws:
- IllegalStateException- If the status line has not be set.
 
- 
getEntityObtains the message entity of this response.The entity is provided by calling setEntity. Notes:- Unlike the HttpResponse.getEntity()method, this method never returns anull response. Instead,getContent().isPresent() can be used to determine whether the response has a body.
 - Specified by:
- getEntityin interface- HttpResponse
- Returns:
- The response entity.  Never null .
 
- Unlike the 
- 
setEntityAssociates a response entity with this response.Notes:- If an entity has already been set for this response and it depends on an input stream
      (HttpEntity.isStreaming()returnstrue ), it must be fully consumed in order to ensure release of resources.
 - Specified by:
- setEntityin interface- HttpResponse
- Parameters:
- entity- The entity to associate with this response, or- null to unset.
 
- If an entity has already been set for this response and it depends on an input stream
      (
- 
getLocaleObtains the locale of this response. The locale is used to determine the reason phrase for the status code. It can be changed usingsetLocale(Locale).- Specified by:
- getLocalein interface- HttpResponse
- Returns:
- The locale of this response, never null .
 
- 
setLocaleChanges the locale of this response.- Specified by:
- setLocalein interface- HttpResponse
- Parameters:
- loc- The new locale.
 
- 
getProtocolVersionReturns the protocol version this message is compatible with.- Specified by:
- getProtocolVersionin interface- HttpMessage
- Returns:
- The protocol version this message is compatible with.
 
- 
containsHeaderChecks if a certain header is present in this message.Header values are ignored. - Specified by:
- containsHeaderin interface- HttpMessage
- Parameters:
- name- The header name to check for.
- Returns:
- true if at least one header with this name is present.
 
- 
getHeadersReturns all the headers with a specified name of this message. Header values are ignored.
 Headers are ordered in the sequence they were sent over a connection.- Specified by:
- getHeadersin interface- HttpMessage
- Parameters:
- name- The name of the headers to return.
- Returns:
- All the headers with a specified name of this message.
 
- 
getFirstHeaderReturns the first header with a specified name of this message.If there is more than one matching header in the message the first element of getHeaders(String)is returned.This method always returns a value so that you can perform assertions on the result. - Specified by:
- getFirstHeaderin interface- HttpMessage
- Parameters:
- name- The name of the header to return.
- Returns:
- The header, never null .
 
- 
getLastHeaderReturns the last header with a specified name of this message.If there is more than one matching header in the message the last element of getHeaders(String)is returned.This method always returns a value so that you can perform assertions on the result. - Specified by:
- getLastHeaderin interface- HttpMessage
- Parameters:
- name- The name of the header to return.
- Returns:
- The header, never null .
 
- 
getHeaderReturns the response header with the specified name.If more that one header with the given name exists the values will be combined with ", " as per RFC 2616 Section 4.2.- Parameters:
- name- The name of the header to return.
- Returns:
- The header, never null .
 
- 
getAllHeadersReturns all the headers of this message. Headers are ordered in the sequence they were sent over a connection.- Specified by:
- getAllHeadersin interface- HttpMessage
- Returns:
- All the headers of this message.
 
- 
addHeaderAdds a header to this message. The header will be appended to the end of the list.- Specified by:
- addHeaderin interface- HttpMessage
- Parameters:
- header- The header to append.
 
- 
addHeaderAdds a header to this message. The header will be appended to the end of the list.- Specified by:
- addHeaderin interface- HttpMessage
- Parameters:
- name- The name of the header.
- value- The value of the header.
 
- 
setHeaderOverwrites the first header with the same name. The new header will be appended to the end of the list, if no header with the given name can be found.- Specified by:
- setHeaderin interface- HttpMessage
- Parameters:
- header- The header to set.
 
- 
setHeaderOverwrites the first header with the same name. The new header will be appended to the end of the list, if no header with the given name can be found.- Specified by:
- setHeaderin interface- HttpMessage
- Parameters:
- name- The name of the header.
- value- The value of the header.
 
- 
setHeadersOverwrites all the headers in the message.- Specified by:
- setHeadersin interface- HttpMessage
- Parameters:
- headers- The array of headers to set.
 
- 
removeHeaderRemoves a header from this message.- Specified by:
- removeHeaderin interface- HttpMessage
- Parameters:
- header- The header to remove.
 
- 
removeHeadersRemoves all headers with a certain name from this message.- Specified by:
- removeHeadersin interface- HttpMessage
- Parameters:
- name- The name of the headers to remove.
 
- 
headerIteratorReturns an iterator of all the headers.- Specified by:
- headerIteratorin interface- HttpMessage
- Returns:
- Iteratorthat returns- Headerobjects in the sequence they are sent over a connection.
 
- 
headerIteratorReturns an iterator of the headers with a given name.- Specified by:
- headerIteratorin interface- HttpMessage
- Parameters:
- name- The name of the headers over which to iterate, or- null for all headers.
- Returns:
- Iteratorthat returns- Headerobjects with the argument name in the sequence they are sent over a connection.
 
- 
getParamsDeprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .Returns the parameters effective for this message as set bysetParams(HttpParams).- Specified by:
- getParamsin interface- HttpMessage
- Returns:
- The parameters effective for this message as set by setParams(HttpParams).
 
- 
setParamsDeprecated.Use configuration classes providedorg.apache.http.config andorg.apache.http.client.config .Provides parameters to be used for the processing of this message.- Specified by:
- setParamsin interface- HttpMessage
- Parameters:
- params- The parameters.
 
- 
closeCloses this response.This method is idempotent and can be called multiple times without side effects. Implementation Notes:This implementation represents a compromise between strict AutoCloseable compliance and debuggability: - Unchecked exceptions (RuntimeExceptionandError) from interceptors are allowed to propagate. This ensures programming errors and serious issues are visible during development and testing.
- Checked exceptions (including RestCallException) are caught and logged but not thrown. This follows AutoCloseable best practices and prevents close exceptions from interfering with try-with-resources cleanup or masking the original exception.
 - Specified by:
- closein interface- AutoCloseable
 
- Unchecked exceptions (
- 
getPartParserSessionCreates a session of the specified part parser.- Parameters:
- parser- The parser to create a session for.
- Returns:
- A session of the specified parser.
 
- 
getPartParserSessionCreates a session of the client-default parat parser.- Returns:
- A session of the specified parser.
 
 
-