Class HttpHeaders
- All Implemented Interfaces:
- Serializable
- Direct Known Subclasses:
- WebSocketHttpHeaders
In addition to the regular methods defined by Map, this class offers many common
convenience methods, for example:
- getFirst(String)returns the first value associated with a given header name
- add(String, String)adds a header value to the list of values for a header name
- set(String, String)sets the header value to a single string value
Note that HttpHeaders instances created by the default constructor
treat header names in a case-insensitive manner. Instances created with the
HttpHeaders(MultiValueMap) constructor like those instantiated
internally by the framework to adapt to existing HTTP headers data structures
do guarantee per-header get/set/add operations to be case-insensitive as
mandated by the HTTP specification. However, it is not necessarily how
entries are actually stored, and this can lead to the reported size()
being inflated. Prefer using headerSet() or headerNames()
to ensure a case-insensitive view.
This class is meant to reference "well-known" headers supported by Spring Framework. If your application or library relies on other headers defined in RFCs, please use methods that accept the header name as a parameter.
Since 7.0, this class no longer implements the MultiValueMap
contract.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Sebastien Deleuze, Brian Clozel, Juergen Hoeller, Josh Long, Sam Brannen, Simon Baslé
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe HTTPAcceptheader field name.static final StringThe HTTPAccept-Charsetheader field name.static final StringThe HTTPAccept-Encodingheader field name.static final StringThe HTTPAccept-Languageheader field name.static final StringThe HTTPAccept-Patchheader field name.static final StringThe HTTPAccept-Rangesheader field name.static final StringThe CORSAccess-Control-Allow-Credentialsresponse header field name.static final StringThe CORSAccess-Control-Allow-Headersresponse header field name.static final StringThe CORSAccess-Control-Allow-Methodsresponse header field name.static final StringThe CORSAccess-Control-Allow-Originresponse header field name.static final StringThe CORSAccess-Control-Expose-Headersresponse header field name.static final StringThe CORSAccess-Control-Max-Ageresponse header field name.static final StringThe CORSAccess-Control-Request-Headersrequest header field name.static final StringThe CORSAccess-Control-Request-Methodrequest header field name.static final StringThe HTTPAgeheader field name.static final StringThe HTTPAllowheader field name.static final StringThe HTTPAuthorizationheader field name.static final StringThe HTTPCache-Controlheader field name.static final StringThe HTTPConnectionheader field name.static final StringThe HTTPContent-Dispositionheader field name.static final StringThe HTTPContent-Encodingheader field name.static final StringThe HTTPContent-Languageheader field name.static final StringThe HTTPContent-Lengthheader field name.static final StringThe HTTPContent-Locationheader field name.static final StringThe HTTPContent-Rangeheader field name.static final StringThe HTTPContent-Typeheader field name.static final StringThe HTTPCookieheader field name.static final StringThe HTTPDateheader field name.static final HttpHeadersAn emptyHttpHeadersinstance (immutable).static final StringThe HTTPETagheader field name.static final StringThe HTTPExpectheader field name.static final StringThe HTTPExpiresheader field name.static final StringThe HTTPFromheader field name.static final StringThe HTTPHostheader field name.static final StringThe HTTPIf-Matchheader field name.static final StringThe HTTPIf-Modified-Sinceheader field name.static final StringThe HTTPIf-None-Matchheader field name.static final StringThe HTTPIf-Rangeheader field name.static final StringThe HTTPIf-Unmodified-Sinceheader field name.static final StringThe HTTPLast-Modifiedheader field name.static final StringThe HTTPLinkheader field name.static final StringThe HTTPLocationheader field name.static final StringThe HTTPMax-Forwardsheader field name.static final StringThe HTTPOriginheader field name.static final StringThe HTTPPragmaheader field name.static final StringThe HTTPProxy-Authenticateheader field name.static final StringThe HTTPProxy-Authorizationheader field name.static final StringThe HTTPRangeheader field name.static final StringThe HTTPRefererheader field name.static final StringThe HTTPRetry-Afterheader field name.static final StringThe HTTPServerheader field name.static final StringThe HTTPSet-Cookieheader field name.static final StringThe HTTPSet-Cookie2header field name.static final StringThe HTTPTEheader field name.static final StringThe HTTPTrailerheader field name.static final StringThe HTTPTransfer-Encodingheader field name.static final StringThe HTTPUpgradeheader field name.static final StringThe HTTPUser-Agentheader field name.static final StringThe HTTPVaryheader field name.static final StringThe HTTPViaheader field name.static final StringThe HTTPWarningheader field name.static final StringThe HTTPWWW-Authenticateheader field name.
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct a new, empty instance of theHttpHeadersobject using an underlying case-insensitive map.HttpHeaders(HttpHeaders httpHeaders) Construct a newHttpHeadersinstance by removing any read-only wrapper that may have been previously applied around the givenHttpHeadersviareadOnlyHttpHeaders(HttpHeaders).HttpHeaders(MultiValueMap<String, String> headers) Construct a newHttpHeadersinstance backed by an existing map.
- 
Method SummaryModifier and TypeMethodDescriptionvoidAdd the given, single header value under the given name.voidAdd all the given values under the given name.voidaddAll(HttpHeaders headers) Add all the values of the givenHttpHeadersto the current header.Deprecated, for removal: This API element is subject to removal in a future version.This method is provided for backward compatibility with APIs that would only accept maps.Deprecated, for removal: This API element is subject to removal in a future version.in favor oftoSingleValueMap()which performs a copy but ensures that collection-iterating methods likeentrySet()are case-insensitivevoidclear()Remove all headers from this HttpHeaders instance.voidRemove the well-known"Content-*"HTTP headers.booleancontainsHeader(String headerName) Returnstrueif this HttpHeaders contains an entry for the given header name.booleancontainsHeaderValue(String headerName, String value) Returnstrueif this HttpHeaders contains the given header and its list of values contains the given value.static HttpHeaderscopyOf(HttpHeaders httpHeaders) Create a newHttpHeadersmutable instance and copy all header values given as a parameter.static HttpHeaderscopyOf(MultiValueMap<String, String> headers) Create a newHttpHeadersmutable instance and copy all header values given as a parameter.static StringencodeBasicAuth(String username, String password, @Nullable Charset charset) Encode the given username and password into Basic Authentication credentials.booleanvoidforEach(BiConsumer<? super String, ? super List<String>> action) Perform an action over each header, as when iterated viaheaderSet().static StringformatHeaders(MultiValueMap<String, String> headers) Helps to format HTTP header values, as HTTP header values themselves can contain comma-separated values, can become confusing with regularMapformatting that also uses commas between entries.Get the list of values associated with the given header name, or null.Return the list of acceptable media types, as specified by theAcceptheader.Return the list of acceptable charsets, as specified by theAccept-Charsetheader.Return the language ranges from the "Accept-Language" header.Return the list of acceptable media types forPATCHmethods, as specified by theAccept-Patchheader.booleanReturn the value of theAccess-Control-Allow-Credentialsresponse header.Return the value of theAccess-Control-Allow-Headersresponse header.Return the value of theAccess-Control-Allow-Methodsresponse header.Return the value of theAccess-Control-Allow-Originresponse header.Return the value of theAccess-Control-Expose-Headersresponse header.longReturn the value of theAccess-Control-Max-Ageresponse header.Return the value of theAccess-Control-Request-Headersrequest header.Return the value of theAccess-Control-Request-Methodrequest header.getAllow()Return the set of allowedHTTP methods, as specified by theAllowheader.Return the value of theCache-Controlheader.Return the value of theConnectionheader.Return a parsed representation of the Content-Disposition header.Get the firstLocaleof the content languages, as specified by theContent-Languageheader.longReturn the length of the body in bytes, as specified by theContent-Lengthheader.Return the media type of the body, as specified by theContent-Typeheader.longgetDate()Return the date and time at which the message was created, as specified by theDateheader.getETag()Return the entity tag of the body, as specified by theETagheader.getETagValuesAsList(String name) Retrieve a combined result from the field values of the ETag header.longReturn the date and time at which the message is no longer valid, as specified by theExpiresheader.getFieldValues(String headerName) Retrieve a combined result from the field values of multivalued headers.Return the first header value for the given header name, if any.longgetFirstDate(String headerName) Parse the first header value for the given header name as a date, return -1 if there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.getFirstZonedDateTime(String headerName) Parse the first header value for the given header name as a date, returnnullif there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.getHost()Return the value of theHostheader, if available.Return the value of theIf-Matchheader.longReturn the value of theIf-Modified-Sinceheader.Return the value of theIf-None-Matchheader.longReturn the value of theIf-Unmodified-Sinceheader.longReturn the time the resource was last changed, as specified by theLast-Modifiedheader.Return the (new) location of a resource as specified by theLocationheader.getOrDefault(String headerName, List<String> defaultValue) Get the list of header values for the given header name, or the given default list of values if the header is not present.getOrEmpty(String headerName) Get the list of header values for the given header name, if any.Return the value of theOriginheader.Return the value of thePragmaheader.getRange()Return the value of theRangeheader.Return the value of theUpgradeheader.getValuesAsList(String headerName) Return all values of a given header name, even if this header is set multiple times.getVary()Return the request header names subject to content negotiation.inthashCode()booleanhasHeaderValues(String headerName, List<String> values) Returnstrueif this HttpHeaders contains exactly the given list of values for the given header name.Return the set of header names.Return a view of the headers as an entrySetof key-list pairs.booleanisEmpty()Returnstrueif this HttpHeaders contains no header entry.Set the list of values associated with the given header name.voidPut all the entries from the givenMultiValueMapinto this HttpHeaders.voidputAll(HttpHeaders headers) Put all the entries from the given HttpHeaders into this HttpHeaders.putIfAbsent(String headerName, List<String> headerValues) Set header values for the given header name if that header name isn't already present in this HttpHeaders and returnnull.static HttpHeadersreadOnlyHttpHeaders(HttpHeaders headers) Apply a read-onlyHttpHeaderswrapper around the given headers, if necessary.static HttpHeadersreadOnlyHttpHeaders(MultiValueMap<String, String> headers) Apply a read-onlyHttpHeaderswrapper around the given headers, if necessary.Remove a header from this HttpHeaders instance, and return the associated value list ornullif that header wasn't present.voidSet the given, single header value under the given name.voidSet the list of acceptable media types, as specified by theAcceptheader.voidsetAcceptCharset(List<Charset> acceptableCharsets) Set the list of acceptable charsets, as specified by theAccept-Charsetheader.voidsetAcceptLanguage(List<Locale.LanguageRange> languages) Set the acceptable language ranges, as specified by the Accept-Language header.voidsetAcceptLanguageAsLocales(List<Locale> locales) Variant ofsetAcceptLanguage(List)usingLocale's.voidsetAcceptPatch(List<MediaType> mediaTypes) voidsetAccessControlAllowCredentials(boolean allowCredentials) Set the (new) value of theAccess-Control-Allow-Credentialsresponse header.voidsetAccessControlAllowHeaders(List<String> allowedHeaders) Set the (new) value of theAccess-Control-Allow-Headersresponse header.voidsetAccessControlAllowMethods(List<HttpMethod> allowedMethods) Set the (new) value of theAccess-Control-Allow-Methodsresponse header.voidsetAccessControlAllowOrigin(@Nullable String allowedOrigin) Set the (new) value of theAccess-Control-Allow-Originresponse header.voidsetAccessControlExposeHeaders(List<String> exposedHeaders) Set the (new) value of theAccess-Control-Expose-Headersresponse header.voidsetAccessControlMaxAge(long maxAge) Set the (new) value of theAccess-Control-Max-Ageresponse header.voidsetAccessControlMaxAge(Duration maxAge) Set the (new) value of theAccess-Control-Max-Ageresponse header.voidsetAccessControlRequestHeaders(List<String> requestHeaders) Set the (new) value of theAccess-Control-Request-Headersrequest header.voidsetAccessControlRequestMethod(@Nullable HttpMethod requestMethod) Set the (new) value of theAccess-Control-Request-Methodrequest header.voidSet all single header value from the given Map under each of their corresponding name.voidsetAllow(Set<HttpMethod> allowedMethods) Set the set of allowedHTTP methods, as specified by theAllowheader.voidsetBasicAuth(String encodedCredentials) Set the value of the Authorization header to Basic Authentication based on the given encoded credentials.voidsetBasicAuth(String username, String password) Set the value of the Authorization header to Basic Authentication based on the given username and password.voidsetBasicAuth(String username, String password, @Nullable Charset charset) Set the value of the Authorization header to Basic Authentication based on the given username and password.voidsetBearerAuth(String token) Set the value of the Authorization header to the given Bearer token.voidsetCacheControl(@Nullable String cacheControl) Set the (new) value of theCache-Controlheader.voidsetCacheControl(CacheControl cacheControl) Set a configuredCacheControlinstance as the new value of theCache-Controlheader.voidsetConnection(String connection) Set the (new) value of theConnectionheader.voidsetConnection(List<String> connection) Set the (new) value of theConnectionheader.voidsetContentDisposition(ContentDisposition contentDisposition) Set the Content-Disposition header.voidsetContentDispositionFormData(String name, @Nullable String filename) Set theContent-Dispositionheader when creating a"multipart/form-data"request.voidsetContentLanguage(@Nullable Locale locale) Set theLocaleof the content language, as specified by the Content-Language header.voidsetContentLength(long contentLength) Set the length of the body in bytes, as specified by theContent-Lengthheader.voidsetContentType(@Nullable MediaType mediaType) Set the media type of the body, as specified by theContent-Typeheader.voidsetDate(long date) Set the date and time at which the message was created, as specified by theDateheader.voidSet the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter.voidSet the date and time at which the message was created, as specified by theDateheader.voidsetDate(ZonedDateTime date) Set the date and time at which the message was created, as specified by theDateheader.voidSet the (new) entity tag of the body, as specified by theETagheader.voidsetExpires(long expires) Set the date and time at which the message is no longer valid, as specified by theExpiresheader.voidsetExpires(Instant expires) Set the date and time at which the message is no longer valid, as specified by theExpiresheader.voidsetExpires(ZonedDateTime expires) Set the duration after which the message is no longer valid, as specified by theExpiresheader.voidSet the (new) value of theHostheader.voidsetIfMatch(String ifMatch) Set the (new) value of theIf-Matchheader.voidsetIfMatch(List<String> ifMatchList) Set the (new) value of theIf-Matchheader.voidsetIfModifiedSince(long ifModifiedSince) Set the (new) value of theIf-Modified-Sinceheader.voidsetIfModifiedSince(Instant ifModifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetIfModifiedSince(ZonedDateTime ifModifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetIfNoneMatch(String ifNoneMatch) Set the (new) value of theIf-None-Matchheader.voidsetIfNoneMatch(List<String> ifNoneMatchList) Set the (new) values of theIf-None-Matchheader.voidsetIfUnmodifiedSince(long ifUnmodifiedSince) Set the (new) value of theIf-Unmodified-Sinceheader.voidsetIfUnmodifiedSince(Instant ifUnmodifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetIfUnmodifiedSince(ZonedDateTime ifUnmodifiedSince) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetInstant(String headerName, Instant date) Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter.voidsetLastModified(long lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetLastModified(Instant lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetLastModified(ZonedDateTime lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.voidsetLocation(@Nullable URI location) Set the (new) location of a resource, as specified by theLocationheader.voidSet the (new) value of theOriginheader.voidSet the (new) value of thePragmaheader.voidSets the (new) value of theRangeheader.voidsetUpgrade(@Nullable String upgrade) Set the (new) value of theUpgradeheader.voidSet the request header names (for example, "Accept-Language") for which the response is subject to content negotiation and variances based on the value of those request headers.voidsetZonedDateTime(String headerName, ZonedDateTime date) Set the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter.intsize()Return the number of headers in the collection.protected StringtoCommaDelimitedString(List<String> headerValues) Turn the given list of header values into a comma-delimited result.Return this HttpHeaders as aMapwith the first values for each header name.toString()
- 
Field Details- 
ACCEPT
- 
ACCEPT_CHARSET
- 
ACCEPT_ENCODING
- 
ACCEPT_LANGUAGE
- 
ACCEPT_PATCHThe HTTPAccept-Patchheader field name.- Since:
- 5.3.6
- See Also:
 
- 
ACCEPT_RANGES
- 
ACCESS_CONTROL_ALLOW_CREDENTIALSThe CORSAccess-Control-Allow-Credentialsresponse header field name.- See Also:
 
- 
ACCESS_CONTROL_ALLOW_HEADERSThe CORSAccess-Control-Allow-Headersresponse header field name.- See Also:
 
- 
ACCESS_CONTROL_ALLOW_METHODSThe CORSAccess-Control-Allow-Methodsresponse header field name.- See Also:
 
- 
ACCESS_CONTROL_ALLOW_ORIGINThe CORSAccess-Control-Allow-Originresponse header field name.- See Also:
 
- 
ACCESS_CONTROL_EXPOSE_HEADERSThe CORSAccess-Control-Expose-Headersresponse header field name.- See Also:
 
- 
ACCESS_CONTROL_MAX_AGEThe CORSAccess-Control-Max-Ageresponse header field name.- See Also:
 
- 
ACCESS_CONTROL_REQUEST_HEADERSThe CORSAccess-Control-Request-Headersrequest header field name.- See Also:
 
- 
ACCESS_CONTROL_REQUEST_METHODThe CORSAccess-Control-Request-Methodrequest header field name.- See Also:
 
- 
AGE
- 
ALLOW
- 
AUTHORIZATION
- 
CACHE_CONTROL
- 
CONNECTION
- 
CONTENT_ENCODING
- 
CONTENT_DISPOSITIONThe HTTPContent-Dispositionheader field name.- See Also:
 
- 
CONTENT_LANGUAGE
- 
CONTENT_LENGTH
- 
CONTENT_LOCATION
- 
CONTENT_RANGE
- 
CONTENT_TYPE
- 
COOKIE
- 
DATE
- 
ETAG
- 
EXPECT
- 
EXPIRES
- 
FROM
- 
HOST
- 
IF_MATCH
- 
IF_MODIFIED_SINCEThe HTTPIf-Modified-Sinceheader field name.- See Also:
 
- 
IF_NONE_MATCH
- 
IF_RANGE
- 
IF_UNMODIFIED_SINCEThe HTTPIf-Unmodified-Sinceheader field name.- See Also:
 
- 
LAST_MODIFIED
- 
LINK
- 
LOCATION
- 
MAX_FORWARDS
- 
ORIGIN
- 
PRAGMA
- 
PROXY_AUTHENTICATEThe HTTPProxy-Authenticateheader field name.- See Also:
 
- 
PROXY_AUTHORIZATIONThe HTTPProxy-Authorizationheader field name.- See Also:
 
- 
RANGE
- 
REFERER
- 
RETRY_AFTER
- 
SERVER
- 
SET_COOKIE
- 
SET_COOKIE2
- 
TE
- 
TRAILER
- 
TRANSFER_ENCODINGThe HTTPTransfer-Encodingheader field name.- See Also:
 
- 
UPGRADE
- 
USER_AGENT
- 
VARY
- 
VIA
- 
WARNING
- 
WWW_AUTHENTICATE
- 
EMPTY
 
- 
- 
Constructor Details- 
HttpHeaderspublic HttpHeaders()Construct a new, empty instance of theHttpHeadersobject using an underlying case-insensitive map.
- 
HttpHeadersConstruct a newHttpHeadersinstance backed by an existing map.This constructor is available as an optimization for adapting to existing headers map structures, primarily for internal use within the framework. - Parameters:
- headers- the headers map (expected to operate with case-insensitive keys)
- Since:
- 5.1
 
- 
HttpHeadersConstruct a newHttpHeadersinstance by removing any read-only wrapper that may have been previously applied around the givenHttpHeadersviareadOnlyHttpHeaders(HttpHeaders).Once the writable instance is mutated, the read-only instance is likely to be out of sync and should be discarded. - Parameters:
- httpHeaders- the headers to expose
- Since:
- 7.0
 
 
- 
- 
Method Details- 
copyOfCreate a newHttpHeadersmutable instance and copy all header values given as a parameter.- Parameters:
- headers- the headers to copy
- Since:
- 7.0
 
- 
copyOfCreate a newHttpHeadersmutable instance and copy all header values given as a parameter.- Parameters:
- httpHeaders- the headers to copy
- Since:
- 7.0
 
- 
getOrEmpty
- 
getOrDefaultGet the list of header values for the given header name, or the given default list of values if the header is not present.- Parameters:
- headerName- the header name
- defaultValue- the fallback list if header is not present
- Returns:
- the list of header values, or a default list of values
- Since:
- 7.0
 
- 
setAcceptSet the list of acceptable media types, as specified by theAcceptheader.
- 
getAcceptReturn the list of acceptable media types, as specified by theAcceptheader.Returns an empty list when the acceptable media types are unspecified. 
- 
setAcceptLanguageSet the acceptable language ranges, as specified by the Accept-Language header.- Since:
- 5.0
 
- 
getAcceptLanguageReturn the language ranges from the "Accept-Language" header.If you only need sorted, preferred locales only use getAcceptLanguageAsLocales()or if you need to filter based on a list of supported locales you can pass the returned list toLocale.filter(List, Collection).- Throws:
- IllegalArgumentException- if the value cannot be converted to a language range
- Since:
- 5.0
 
- 
setAcceptLanguageAsLocalesVariant ofsetAcceptLanguage(List)usingLocale's.- Since:
- 5.0
 
- 
getAcceptLanguageAsLocales- Returns:
- the locales or an empty list
- Throws:
- IllegalArgumentException- if the value cannot be converted to a locale
- Since:
- 5.0
 
- 
setAcceptPatch- Since:
- 5.3.6
 
- 
getAcceptPatchReturn the list of acceptable media types forPATCHmethods, as specified by theAccept-Patchheader.Returns an empty list when the acceptable media types are unspecified. - Since:
- 5.3.6
 
- 
setAccessControlAllowCredentialspublic void setAccessControlAllowCredentials(boolean allowCredentials) Set the (new) value of theAccess-Control-Allow-Credentialsresponse header.
- 
getAccessControlAllowCredentialspublic boolean getAccessControlAllowCredentials()Return the value of theAccess-Control-Allow-Credentialsresponse header.
- 
setAccessControlAllowHeaders
- 
getAccessControlAllowHeaders
- 
setAccessControlAllowMethodsSet the (new) value of theAccess-Control-Allow-Methodsresponse header.
- 
getAccessControlAllowMethodsReturn the value of theAccess-Control-Allow-Methodsresponse header.
- 
setAccessControlAllowOrigin
- 
getAccessControlAllowOrigin
- 
setAccessControlExposeHeaders
- 
getAccessControlExposeHeaders
- 
setAccessControlMaxAgeSet the (new) value of theAccess-Control-Max-Ageresponse header.- Since:
- 5.2
 
- 
setAccessControlMaxAgepublic void setAccessControlMaxAge(long maxAge) Set the (new) value of theAccess-Control-Max-Ageresponse header.
- 
getAccessControlMaxAgepublic long getAccessControlMaxAge()Return the value of theAccess-Control-Max-Ageresponse header.Returns -1 when the max age is unknown. 
- 
setAccessControlRequestHeaders
- 
getAccessControlRequestHeaders
- 
setAccessControlRequestMethodSet the (new) value of theAccess-Control-Request-Methodrequest header.
- 
getAccessControlRequestMethodReturn the value of theAccess-Control-Request-Methodrequest header.
- 
setAcceptCharset
- 
getAcceptCharset
- 
setAllowSet the set of allowedHTTP methods, as specified by theAllowheader.
- 
getAllowReturn the set of allowedHTTP methods, as specified by theAllowheader.Returns an empty set when the allowed methods are unspecified. 
- 
setBasicAuthSet the value of the Authorization header to Basic Authentication based on the given username and password.Note that this method only supports characters in the ISO-8859-1character set.- Parameters:
- username- the username
- password- the password
- Throws:
- IllegalArgumentException- if either- useror- passwordcontain characters that cannot be encoded to ISO-8859-1
- Since:
- 5.1
- See Also:
 
- 
setBasicAuthSet the value of the Authorization header to Basic Authentication based on the given username and password.- Parameters:
- username- the username
- password- the password
- charset- the charset to use to convert the credentials into an octet sequence. Defaults to ISO-8859-1.
- Throws:
- IllegalArgumentException- if- usernameor- passwordcontains characters that cannot be encoded to the given charset
- Since:
- 5.1
- See Also:
 
- 
setBasicAuthSet the value of the Authorization header to Basic Authentication based on the given encoded credentials.Favor this method over setBasicAuth(String, String)andsetBasicAuth(String, String, Charset)if you wish to cache the encoded credentials.- Parameters:
- encodedCredentials- the encoded credentials
- Throws:
- IllegalArgumentException- if supplied credentials string is- nullor blank
- Since:
- 5.2
- See Also:
 
- 
setBearerAuthSet the value of the Authorization header to the given Bearer token.- Parameters:
- token- the Base64 encoded token
- Since:
- 5.1
- See Also:
 
- 
setCacheControlSet a configuredCacheControlinstance as the new value of theCache-Controlheader.- Since:
- 5.0.5
 
- 
setCacheControl
- 
getCacheControl
- 
setConnectionSet the (new) value of theConnectionheader.
- 
setConnection
- 
getConnection
- 
setContentDispositionFormDataSet theContent-Dispositionheader when creating a"multipart/form-data"request.Applications typically would not set this header directly but rather prepare a MultiValueMap<String, Object>, containing an Object or aResourcefor each part, and then pass that to theRestTemplateorWebClient.- Parameters:
- name- the control name
- filename- the filename (may be- null)
- See Also:
 
- 
setContentDispositionSet the Content-Disposition header.This could be used on a response to indicate if the content is expected to be displayed inline in the browser or as an attachment to be saved locally. It can also be used for a "multipart/form-data"request. For more details see notes onsetContentDispositionFormData(String, String).- Since:
- 5.0
- See Also:
 
- 
getContentDispositionReturn a parsed representation of the Content-Disposition header.- Since:
- 5.0
- See Also:
 
- 
setContentLanguage
- 
getContentLanguageGet the firstLocaleof the content languages, as specified by theContent-Languageheader.Use getValuesAsList(String)if you need to get multiple content languages.- Returns:
- the first Localeof the content languages, ornullif unknown
- Since:
- 5.0
 
- 
setContentLengthpublic void setContentLength(long contentLength) Set the length of the body in bytes, as specified by theContent-Lengthheader.- Parameters:
- contentLength- content length (greater than or equal to zero)
- Throws:
- IllegalArgumentException- if the content length is negative
 
- 
getContentLengthpublic long getContentLength()Return the length of the body in bytes, as specified by theContent-Lengthheader.Returns -1 when the content-length is unknown. 
- 
setContentTypeSet the media type of the body, as specified by theContent-Typeheader.
- 
getContentTypeReturn the media type of the body, as specified by theContent-Typeheader.Returns nullwhen theContent-Typeheader is not set.- Throws:
- InvalidMediaTypeException- if the media type value cannot be parsed
 
- 
setDateSet the date and time at which the message was created, as specified by theDateheader.- Since:
- 5.2
 
- 
setDateSet the date and time at which the message was created, as specified by theDateheader.- Since:
- 5.2
 
- 
setDatepublic void setDate(long date) Set the date and time at which the message was created, as specified by theDateheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT. 
- 
getDatepublic long getDate()Return the date and time at which the message was created, as specified by theDateheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown. - Throws:
- IllegalArgumentException- if the value cannot be converted to a date
 
- 
setETag
- 
getETag
- 
setExpiresSet the duration after which the message is no longer valid, as specified by theExpiresheader.- Since:
- 5.0.5
 
- 
setExpiresSet the date and time at which the message is no longer valid, as specified by theExpiresheader.- Since:
- 5.2
 
- 
setExpirespublic void setExpires(long expires) Set the date and time at which the message is no longer valid, as specified by theExpiresheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT. 
- 
getExpirespublic long getExpires()Return the date and time at which the message is no longer valid, as specified by theExpiresheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown. - See Also:
 
- 
setHost
- 
getHostReturn the value of theHostheader, if available.If the header value does not contain a port, the port in the returned address will be 0.- Since:
- 5.0
 
- 
setIfMatch
- 
setIfMatch
- 
getIfMatchReturn the value of theIf-Matchheader.- Throws:
- IllegalArgumentException- if parsing fails
- Since:
- 4.3
 
- 
setIfModifiedSinceSet the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
 
- 
setIfModifiedSinceSet the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
 
- 
setIfModifiedSincepublic void setIfModifiedSince(long ifModifiedSince) Set the (new) value of theIf-Modified-Sinceheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT. 
- 
getIfModifiedSincepublic long getIfModifiedSince()Return the value of theIf-Modified-Sinceheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown. - See Also:
 
- 
setIfNoneMatchSet the (new) value of theIf-None-Matchheader.
- 
setIfNoneMatch
- 
getIfNoneMatchReturn the value of theIf-None-Matchheader.- Throws:
- IllegalArgumentException- if parsing fails
 
- 
setIfUnmodifiedSinceSet the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
 
- 
setIfUnmodifiedSinceSet the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
 
- 
setIfUnmodifiedSincepublic void setIfUnmodifiedSince(long ifUnmodifiedSince) Set the (new) value of theIf-Unmodified-Sinceheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT. - Since:
- 4.3
 
- 
getIfUnmodifiedSincepublic long getIfUnmodifiedSince()Return the value of theIf-Unmodified-Sinceheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown. - Since:
- 4.3
- See Also:
 
- 
setLastModifiedSet the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
 
- 
setLastModifiedSet the time the resource was last changed, as specified by theLast-Modifiedheader.- Since:
- 5.1.4
 
- 
setLastModifiedpublic void setLastModified(long lastModified) Set the time the resource was last changed, as specified by theLast-Modifiedheader.The date should be specified as the number of milliseconds since January 1, 1970 GMT. 
- 
getLastModifiedpublic long getLastModified()Return the time the resource was last changed, as specified by theLast-Modifiedheader.The date is returned as the number of milliseconds since January 1, 1970 GMT. Returns -1 when the date is unknown. - See Also:
 
- 
setLocation
- 
getLocation
- 
setOrigin
- 
getOrigin
- 
setPragma
- 
getPragma
- 
setRange
- 
getRange
- 
setUpgrade
- 
getUpgrade
- 
setVary
- 
getVary
- 
setZonedDateTimeSet the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter. The equivalent ofset(String, String)but for date headers.- Since:
- 5.0
 
- 
setInstantSet the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter. The equivalent ofset(String, String)but for date headers.- Since:
- 5.1.4
 
- 
setDateSet the given date under the given header name after formatting it as a string using the RFC-1123 date-time formatter. The equivalent ofset(String, String)but for date headers.- Since:
- 3.2.4
- See Also:
 
- 
getFirstDateParse the first header value for the given header name as a date, return -1 if there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.- Parameters:
- headerName- the header name
- Returns:
- the parsed date header, or -1 if none
- Since:
- 3.2.4
- See Also:
 
- 
getFirstZonedDateTimeParse the first header value for the given header name as a date, returnnullif there is no value, or raiseIllegalArgumentExceptionif the value cannot be parsed as a date.- Parameters:
- headerName- the header name
- Returns:
- the parsed date header, or nullif none
- Since:
- 5.0
 
- 
getValuesAsListReturn all values of a given header name, even if this header is set multiple times.This method supports double-quoted values, as described in RFC 9110, section 5.5. - Parameters:
- headerName- the header name
- Returns:
- all associated values
- Since:
- 4.3
 
- 
clearContentHeaderspublic void clearContentHeaders()Remove the well-known"Content-*"HTTP headers.Such headers should be cleared from the response if the intended body can't be written due to errors. - Since:
- 5.2.3
 
- 
getETagValuesAsListRetrieve a combined result from the field values of the ETag header.- Parameters:
- name- the header name
- Returns:
- the combined result
- Throws:
- IllegalArgumentException- if parsing fails
- Since:
- 4.3
 
- 
getFieldValues
- 
toCommaDelimitedString
- 
getFirst
- 
addAdd the given, single header value under the given name.- Parameters:
- headerName- the header name
- headerValue- the header value
- Throws:
- UnsupportedOperationException- if adding headers is not supported
- See Also:
 
- 
addAllAdd all the given values under the given name.As values are represented as a List, duplicate values can be introduced. Seeput(String, List)to replace the list of values instead.- Parameters:
- headerName- the header name
- headerValues- the values to add
- See Also:
 
- 
addAllAdd all the values of the givenHttpHeadersto the current header.As values are represented as a List, duplicate values can be introduced. SeeputAll(HttpHeaders)to replace the list of values of each individual header name instead.- Parameters:
- headers- the headers to add
- Since:
- 7.0
- See Also:
 
- 
setSet the given, single header value under the given name.- Parameters:
- headerName- the header name
- headerValue- the header value
- Throws:
- UnsupportedOperationException- if adding headers is not supported
- See Also:
 
- 
setAll
- 
toSingleValueMapReturn this HttpHeaders as aMapwith the first values for each header name.The difference between this method and asSingleValueMap()is that this method returns a copy of the headers, whereas the latter returns a view. This copy also ensures that collection-iterating methods likeentrySet()are case-insensitive.- Returns:
- a single value representation of these headers
 
- 
asSingleValueMapDeprecated, for removal: This API element is subject to removal in a future version.in favor oftoSingleValueMap()which performs a copy but ensures that collection-iterating methods likeentrySet()are case-insensitiveReturn this HttpHeaders as aMapwith the first values for each header name.The difference between this method and toSingleValueMap()is that this method returns a view of the headers, whereas the latter returns a copy. This method is also susceptible to include multiple casing variants of a given header name, seeasMultiValueMap()javadoc.- Returns:
- a single value representation of these headers
 
- 
asMultiValueMapDeprecated, for removal: This API element is subject to removal in a future version.This method is provided for backward compatibility with APIs that would only accept maps. Generally avoid using HttpHeaders as a Map or MultiValueMap.Return this HttpHeaders as aMultiValueMapwith the full list of values for each header name.Note that some backing server headers implementations can store header names in a case-sensitive manner, which will lead to duplicates during iteration in methods like entrySet(), where multiple occurrences of a header name can surface depending on letter casing but each such entry has the fullListof values.- Returns:
- a MultiValueMap representation of these headers
- Since:
- 7.0
 
- 
isEmptypublic boolean isEmpty()Returnstrueif this HttpHeaders contains no header entry.
- 
containsHeaderReturnstrueif this HttpHeaders contains an entry for the given header name.- Parameters:
- headerName- the header name
- Since:
- 7.0
 
- 
hasHeaderValues
- 
containsHeaderValue
- 
getGet the list of values associated with the given header name, or null.To ensure support for double-quoted values, see also getValuesAsList(String).- Parameters:
- headerName- the header name
- Since:
- 7.0
- See Also:
 
- 
putSet the list of values associated with the given header name. Returns the previous list of values, ornullif the header was not present.- Parameters:
- headerName- the header name
- headerValues- the new values
- Returns:
- the old values for the given header name
 
- 
putIfAbsentSet header values for the given header name if that header name isn't already present in this HttpHeaders and returnnull. If the header is already present, returns the associated value list instead.- Parameters:
- headerName- the header name
- headerValues- the header values to set if header is not present
- Returns:
- the previous value or null
 
- 
putAllPut all the entries from the given HttpHeaders into this HttpHeaders.- Parameters:
- headers- the given headers
- Since:
- 7.0
- See Also:
 
- 
putAll
- 
remove
- 
clearpublic void clear()Remove all headers from this HttpHeaders instance.
- 
sizepublic int size()Return the number of headers in the collection. This can be inflated, seeclass level javadoc.
- 
forEachPerform an action over each header, as when iterated viaheaderSet().- Parameters:
- action- the action to be performed for each entry
 
- 
headerSetReturn a view of the headers as an entrySetof key-list pairs. BothIterator.remove()andMap.Entry.setValue(V)are supported and mutate the headers.This collection is guaranteed to contain one entry per header name even if the backing structure stores multiple casing variants of names, at the cost of first copying the names into a case-insensitive set for filtering the iteration. - Returns:
- a Setview that iterates over all headers in a case-insensitive manner
- Since:
- 6.1.15
 
- 
headerNamesReturn the set of header names. BothSet.remove(Object)andSet.clear()operations are supported and mutate the headers.This collection is guaranteed to contain only one casing variant of each header name even if the backing structure stores multiple casing variants of names. The first encountered variant is the one that is retained. - Returns:
- a Setof all the headers names
- Since:
- 7.0
 
- 
equals
- 
hashCode
- 
toString
- 
readOnlyHttpHeadersApply a read-onlyHttpHeaderswrapper around the given headers, if necessary.Also caches the parsed representations of the "Accept" and "Content-Type" headers. - Parameters:
- headers- the headers to expose
- Returns:
- a read-only variant of the headers, or the original headers as-is
(in case it happens to be a read-only HttpHeadersinstance already)
- Since:
- 5.3
 
- 
readOnlyHttpHeadersApply a read-onlyHttpHeaderswrapper around the given headers, if necessary.Also caches the parsed representations of the "Accept" and "Content-Type" headers. - Parameters:
- headers- the headers to expose
- Returns:
- a read-only variant of the headers, or the original headers as-is if already read-only
 
- 
formatHeadersHelps to format HTTP header values, as HTTP header values themselves can contain comma-separated values, can become confusing with regularMapformatting that also uses commas between entries.Additionally, this method displays the native list of header names with the mention with native header namesif the underlying implementation stores multiple casing variants of header names (seeclass level javadoc).- Parameters:
- headers- the headers to format
- Returns:
- the headers to a String
- Since:
- 5.1.4
 
- 
encodeBasicAuthEncode the given username and password into Basic Authentication credentials.The encoded credentials returned by this method can be supplied to setBasicAuth(String)to set the Basic Authentication header.- Parameters:
- username- the username
- password- the password
- charset- the charset to use to convert the credentials into an octet sequence. Defaults to ISO-8859-1.
- Throws:
- IllegalArgumentException- if- usernameor- passwordcontains characters that cannot be encoded to the given charset
- Since:
- 5.2
- See Also:
 
 
-