Interface ResponseHeaders


public interface ResponseHeaders
This interface provides access to HTTP response headers in a way that is independent of any specific HTTP client implementation. Headers are represented as an ordered list of name-value pairs, preserving the original structure including duplicate header names.

Implementations of this interface should be immutable and thread-safe.

Since:
4.2.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Represents a single HTTP header as a name-value pair.
  • Method Summary

    Modifier and Type
    Method
    Description
    get(int index)
    Returns the header at the specified index.
    boolean
    Returns true if this collection contains no headers.
    int
    Returns the number of headers in this collection.
    value(String name)
    Returns the value of the first header with the specified name (case-insensitive), or null if no such header exists.
  • Method Details

    • size

      int size()
      Returns the number of headers in this collection.

      Note that if a header name appears multiple times (e.g., multiple Set-Cookie headers), each appearance is counted separately.

      Returns:
      the number of headers
    • get

      ResponseHeaders.Header get(int index)
      Returns the header at the specified index.

      Headers are indexed in the order they appeared in the HTTP response.

      Parameters:
      index - the index of the header to retrieve (0-based)
      Returns:
      the header at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is out of range
    • value

      String value(String name)
      Returns the value of the first header with the specified name (case-insensitive), or null if no such header exists.

      This is a convenience method for headers where you expect a single value. If the header appears multiple times in the response, only the first occurrence is returned.

      Note that the returned value may contain commas if:

      • The original HTTP response had a comma in the header value, or
      • The HTTP client combined multiple header lines into one (though this is uncommon with the underlying OkHttp implementation)

      For headers that can legitimately appear multiple times (like Set-Cookie), use size() and get(int) to iterate through all occurrences instead.

      Example usage:

       String contentType = headers.value("Content-Type");
       String retryAfter = headers.value("Retry-After");
       
      Parameters:
      name - the header name (case-insensitive)
      Returns:
      the value of the first matching header, or null if not found
    • isEmpty

      boolean isEmpty()
      Returns true if this collection contains no headers.
      Returns:
      true if there are no headers