HttpHeaders Class
- java.
lang. Object - com.
azure. core. http. HttpHeaders
- com.
Implements
public class HttpHeaders
implements Iterable<HttpHeader>
Represents a collection of headers on an HTTP request or response.
This class encapsulates the headers of an HTTP request or response. It provides methods to add, set, get, and remove headers. It also provides methods to convert the headers to a Map, and to get a Stream representation of the headers.
Each header is represented by an HttpHeader instance, which encapsulates the name and value(s) of a header. If multiple values are associated with the same header name, they are stored in a single HttpHeader instance with values separated by commas.
Note: Header names are case-insensitive.
Constructor Summary
Constructor | Description |
---|---|
HttpHeaders() |
Create an empty Http |
HttpHeaders(int initialCapacity) |
Create a Http |
HttpHeaders(Iterable<HttpHeader> headers) |
Create a Http |
HttpHeaders(Map<String,String> headers) |
Create a Http |
Method Summary
Modifier and Type | Method and Description |
---|---|
Http |
add(HttpHeaderName name, String value)
Adds a HttpHeader with the given name and value if a header with that name doesn't already exist, otherwise adds the |
Http |
add(String name, String value)
Deprecated
Use add(HttpHeaderName name, String value) as it provides better performance.
Adds a HttpHeader with the given name and value if a header with that name doesn't already exist, otherwise adds the |
Http |
get(HttpHeaderName name)
Gets the HttpHeader for the provided header name. |
Http |
get(String name)
Deprecated
Use get(HttpHeaderName name) as it provides better performance.
Gets the HttpHeader for the provided header name. |
int |
getSize()
Gets the number of headers in the collection. |
String |
getValue(HttpHeaderName name)
Get the value for the provided header name. |
String |
getValue(String name)
Deprecated
Use getValue(HttpHeaderName name) as it provides better performance.
Get the value for the provided header name. |
String[] |
getValues(HttpHeaderName name)
Get the values for the provided header name. |
String[] |
getValues(String name)
Deprecated
Use getValue(HttpHeaderName name) as it provides better performance.
Get the values for the provided header name. |
Iterator<Http |
iterator() |
Http |
put(String name, String value)
Deprecated
Use set(HttpHeaderName name, String value) instead.
Sets a HttpHeader with the given name and value. |
Http |
remove(HttpHeaderName name)
Removes the HttpHeader with the provided header name. |
Http |
remove(String name)
Deprecated
Use remove(HttpHeaderName name) as it provides better performance.
Removes the HttpHeader with the provided header name. |
Http |
set(HttpHeaderName name, String value)
Sets a HttpHeader with the given name and value. |
Http |
set(HttpHeaderName name, List<String> values)
Sets a HttpHeader with the given name and the list of values provided, such that the given values will be comma-separated when necessary. |
Http |
set(String name, String value)
Deprecated
Use set(HttpHeaderName name, String value) as it provides better performance.
Sets a HttpHeader with the given name and value. |
Http |
set(String name, List<String> values)
Deprecated
Use set(HttpHeaderName name, List<String> values) as it provides better performance.
Sets a HttpHeader with the given name and the list of values provided, such that the given values will be comma-separated when necessary. |
Http |
setAll(Map<String,List<String>> headers)
Sets all provided header key/values pairs into this Http |
Http |
setAllHttpHeaders(HttpHeaders headers)
Sets all headers from the passed |
Stream<Http |
stream()
Get a Stream representation of the Http |
Map<String,String> |
toMap()
Returns a copy of the http headers as an unmodifiable Map representation of the state of the headers at the time of the to |
String | toString() |
Methods inherited from java.lang.Object
Constructor Details
HttpHeaders
public HttpHeaders()
Create an empty HttpHeaders instance.
HttpHeaders
public HttpHeaders(int initialCapacity)
Create a HttpHeaders instance with an initial size
empty headers
Parameters:
HttpHeaders
public HttpHeaders(Iterable
Create a HttpHeaders instance with the provided initial headers.
Parameters:
HttpHeaders
public HttpHeaders(Map
Create a HttpHeaders instance with the provided initial headers.
Parameters:
Method Details
add
public HttpHeaders add(HttpHeaderName name, String value)
Adds a HttpHeader with the given name and value if a header with that name doesn't already exist, otherwise adds the value
to the existing header.
Parameters:
Returns:
add
@Deprecated
public HttpHeaders add(String name, String value)
Deprecated
Adds a HttpHeader with the given name and value if a header with that name doesn't already exist, otherwise adds the value
to the existing header.
Parameters:
Returns:
get
public HttpHeader get(HttpHeaderName name)
Gets the HttpHeader for the provided header name. null is returned if the header isn't found.
Parameters:
Returns:
get
@Deprecated
public HttpHeader get(String name)
Deprecated
Gets the HttpHeader for the provided header name. null is returned if the header isn't found.
Parameters:
Returns:
getSize
public int getSize()
Gets the number of headers in the collection.
Returns:
getValue
public String getValue(HttpHeaderName name)
Get the value for the provided header name. null is returned if the header name isn't found.
Parameters:
Returns:
getValue
@Deprecated
public String getValue(String name)
Deprecated
Get the value for the provided header name. null is returned if the header name isn't found.
Parameters:
Returns:
getValues
public String[] getValues(HttpHeaderName name)
Get the values for the provided header name. null is returned if the header name isn't found.
This returns getValue(String name) split by comma
.
Parameters:
Returns:
getValues
@Deprecated
public String[] getValues(String name)
Deprecated
Get the values for the provided header name. null is returned if the header name isn't found.
This returns getValue(String name) split by comma
.
Parameters:
Returns:
iterator
public Iterator
put
@Deprecated
public HttpHeaders put(String name, String value)
Deprecated
Sets a HttpHeader with the given name and value.
If header with same name already exists then the value will be overwritten.
Parameters:
Returns:
remove
public HttpHeader remove(HttpHeaderName name)
Removes the HttpHeader with the provided header name. null is returned if the header isn't found.
Parameters:
Returns:
remove
@Deprecated
public HttpHeader remove(String name)
Deprecated
Removes the HttpHeader with the provided header name. null is returned if the header isn't found.
Parameters:
Returns:
set
public HttpHeaders set(HttpHeaderName name, String value)
Sets a HttpHeader with the given name and value. If a header with same name already exists then the value will be overwritten. If the given value is null, the header with the given name will be removed.
Parameters:
Returns:
set
public HttpHeaders set(HttpHeaderName name, List
Sets a HttpHeader with the given name and the list of values provided, such that the given values will be comma-separated when necessary. If a header with same name already exists then the values will be overwritten. If the given values list is null, the header with the given name will be removed.
Parameters:
Returns:
set
@Deprecated
public HttpHeaders set(String name, String value)
Deprecated
Sets a HttpHeader with the given name and value. If a header with same name already exists then the value will be overwritten. If the given value is null, the header with the given name will be removed.
Parameters:
Returns:
set
@Deprecated
public HttpHeaders set(String name, List
Deprecated
Sets a HttpHeader with the given name and the list of values provided, such that the given values will be comma-separated when necessary. If a header with same name already exists then the values will be overwritten. If the given values list is null, the header with the given name will be removed.
Parameters:
Returns:
setAll
public HttpHeaders setAll(Map
Sets all provided header key/values pairs into this HttpHeaders instance. This is equivalent to calling headers.forEach(this::set)
, and therefore the behavior is as specified in set(String name, List<String> values). In other words, this will create a header for each key in the provided map, replacing or removing an existing one, depending on the value. If the given values list is null, the header with the given name will be removed. If the given name is already a header, it will be removed and replaced with the headers provided.
Use setAllHttpHeaders(HttpHeaders headers) if you already have an instance of HttpHeaders as it provides better performance.
Parameters:
Returns:
setAllHttpHeaders
public HttpHeaders setAllHttpHeaders(HttpHeaders headers)
Sets all headers from the passed headers
into this HttpHeaders.
This is the equivalent to calling headers.forEach(header -> set(header.getName(), header.getValuesList())
and therefore the behavior is as specified in set(String name, List<String> values).
If headers
is null this is a no-op.
Parameters:
Returns:
stream
public Stream
Get a Stream representation of the HttpHeader values in this instance.
Returns:
toMap
public Map
Returns a copy of the http headers as an unmodifiable Map representation of the state of the headers at the time of the toMap call. This map will not change as the underlying http headers change, and nor will modifying the key or values contained in the map have any effect on the state of the http headers.
Note that there may be performance implications of using Map APIs on the returned Map. It is highly recommended that users prefer to use alternate APIs present on the HttpHeaders class, over using APIs present on the returned Map class. For example, use the get(String name) API, rather than httpHeaders.toMap().get(name)
.
Returns:
toString
public String toString()
Overrides:
HttpHeaders.toString()Applies to
Azure SDK for Java