Class CustomHeadersInterceptor

  • All Implemented Interfaces:
    okhttp3.Interceptor

    public final class CustomHeadersInterceptor
    extends Object
    implements okhttp3.Interceptor
    An instance of this class enables adding custom headers in client requests when added to the OkHttpClient interceptors.
    • Constructor Detail

      • CustomHeadersInterceptor

        public CustomHeadersInterceptor​(String key,
                                        String value)
        Initialize an instance of CustomHeadersInterceptor class.
        Parameters:
        key - the key for the header
        value - the value of the header
    • Method Detail

      • headers

        public Map<String,​List<String>> headers()
        Returns:
        the currently stored custom headers
      • replaceHeader

        public CustomHeadersInterceptor replaceHeader​(String name,
                                                      String value)
        Add a single header key-value pair. If one with the name already exists, it gets replaced.
        Parameters:
        name - the name of the header.
        value - the value of the header.
        Returns:
        the interceptor instance itself.
      • addHeader

        public CustomHeadersInterceptor addHeader​(String name,
                                                  String value)
        Add a single header key-value pair. If one with the name already exists, both stay in the header map.
        Parameters:
        name - the name of the header.
        value - the value of the header.
        Returns:
        the interceptor instance itself.
      • addHeaders

        public CustomHeadersInterceptor addHeaders​(okhttp3.Headers headers)
        Add all headers in a Headers object.
        Parameters:
        headers - an OkHttp Headers object.
        Returns:
        the interceptor instance itself.
      • addHeaderMap

        public CustomHeadersInterceptor addHeaderMap​(Map<String,​String> headers)
        Add all headers in a header map.
        Parameters:
        headers - a map of headers.
        Returns:
        the interceptor instance itself.
      • addHeaderMultimap

        public CustomHeadersInterceptor addHeaderMultimap​(Map<String,​List<String>> headers)
        Add all headers in a header multimap.
        Parameters:
        headers - a multimap of headers.
        Returns:
        the interceptor instance itself.
      • removeHeader

        public CustomHeadersInterceptor removeHeader​(String name)
        Remove a header.
        Parameters:
        name - the name of the header to remove.
        Returns:
        the interceptor instance itself.
      • intercept

        public okhttp3.Response intercept​(okhttp3.Interceptor.Chain chain)
                                   throws IOException
        Specified by:
        intercept in interface okhttp3.Interceptor
        Throws:
        IOException