org.apache.hc.core5.http

## Class ContentType

- java.lang.Object
- - org.apache.hc.core5.http.ContentType

- All Implemented Interfaces:
    Serializable

      @Contract(threading=IMMUTABLE)
      public final class ContentType
      extends Object
      implements Serializable

  Content type information consisting of a MIME type and an optional charset.

  This class makes no attempts to verify validity of the MIME type. The input parameters of the `create(String, String)` method, however, may not contain characters `<">, <;>, <,>` reserved by the HTTP specification.

  Since:
    4.2
  See Also:
    Serialized Form

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static ContentType` | `APPLICATION_ATOM_XML`
    `static ContentType` | `APPLICATION_FORM_URLENCODED`
    `static ContentType` | `APPLICATION_JSON`
    `static ContentType` | `APPLICATION_NDJSON` Public constant media type for `application/x-ndjson`.
    `static ContentType` | `APPLICATION_OCTET_STREAM`
    `static ContentType` | `APPLICATION_PDF` Public constant media type for `application/pdf`.
    `static ContentType` | `APPLICATION_PROBLEM_JSON` Public constant media type for `application/problem+json`.
    `static ContentType` | `APPLICATION_PROBLEM_XML` Public constant media type for `application/problem+xml`.
    `static ContentType` | `APPLICATION_RSS_XML` Public constant media type for `application/rss+xml`.
    `static ContentType` | `APPLICATION_SOAP_XML`
    `static ContentType` | `APPLICATION_SVG_XML`
    `static ContentType` | `APPLICATION_XHTML_XML`
    `static ContentType` | `APPLICATION_XML`
    `static ContentType` | `DEFAULT_BINARY`
    `static ContentType` | `DEFAULT_TEXT`
    `static ContentType` | `IMAGE_BMP`
    `static ContentType` | `IMAGE_GIF`
    `static ContentType` | `IMAGE_JPEG`
    `static ContentType` | `IMAGE_PNG`
    `static ContentType` | `IMAGE_SVG`
    `static ContentType` | `IMAGE_TIFF`
    `static ContentType` | `IMAGE_WEBP`
    `static ContentType` | `MULTIPART_FORM_DATA`
    `static ContentType` | `MULTIPART_MIXED` Public constant media type for `multipart/mixed`.
    `static ContentType` | `MULTIPART_RELATED` Public constant media type for `multipart/related`.
    `static ContentType` | `TEXT_EVENT_STREAM` Public constant media type for `text/event-stream`.
    `static ContentType` | `TEXT_HTML`
    `static ContentType` | `TEXT_MARKDOWN` Public constant media type for `text/markdown`.
    `static ContentType` | `TEXT_PLAIN`
    `static ContentType` | `TEXT_XML`
    `static ContentType` | `WILDCARD`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static ContentType` | `create(String mimeType)` Creates a new instance of `ContentType` without a charset.
    `static ContentType` | `create(String mimeType, Charset charset)` Creates a new instance of `ContentType`.
    `static ContentType` | `create(String mimeType, NameValuePair... params)` Creates a new instance of `ContentType` with the given parameters.
    `static ContentType` | `create(String mimeType, String charset)` Creates a new instance of `ContentType`.
    `static ContentType` | `getByMimeType(String mimeType)` Deprecated. Do not use. This method was made public by mistake.
    `Charset` | `getCharset()`
    `String` | `getMimeType()`
    `String` | `getParameter(String name)`
    `boolean` | `isSameMimeType(ContentType contentType)`
    `static ContentType` | `parse(CharSequence s)` Parses textual representation of `Content-Type` value.
    `static ContentType` | `parseLenient(CharSequence s)` Parses textual representation of `Content-Type` value ignoring invalid charsets.
    `String` | `toString()` Generates textual representation of this content type which can be used as the value of a `Content-Type` header.
    `ContentType` | `withCharset(Charset charset)` Creates a new instance with this MIME type and the given Charset.
    `ContentType` | `withCharset(String charset)` Creates a new instance with this MIME type and the given Charset name.
    `ContentType` | `withParameters(NameValuePair... params)` Creates a new instance with this MIME type and the given parameters.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### APPLICATION_ATOM_XML

          public static final ContentType APPLICATION_ATOM_XML

    - #### APPLICATION_FORM_URLENCODED

          public static final ContentType APPLICATION_FORM_URLENCODED

    - #### APPLICATION_JSON

          public static final ContentType APPLICATION_JSON

    - #### APPLICATION_NDJSON

          public static final ContentType APPLICATION_NDJSON

      Public constant media type for `application/x-ndjson`.
      Since:
        5.1

    - #### APPLICATION_OCTET_STREAM

          public static final ContentType APPLICATION_OCTET_STREAM

    - #### APPLICATION_PDF

          public static final ContentType APPLICATION_PDF

      Public constant media type for `application/pdf`.
      Since:
        5.1

    - #### APPLICATION_SOAP_XML

          public static final ContentType APPLICATION_SOAP_XML

    - #### APPLICATION_SVG_XML

          public static final ContentType APPLICATION_SVG_XML

    - #### APPLICATION_XHTML_XML

          public static final ContentType APPLICATION_XHTML_XML

    - #### APPLICATION_XML

          public static final ContentType APPLICATION_XML

    - #### APPLICATION_PROBLEM_JSON

          public static final ContentType APPLICATION_PROBLEM_JSON

      Public constant media type for `application/problem+json`.
      Since:
        5.1
      See Also:
        Problem Details for HTTP APIs, 6.1. application/problem+json

    - #### APPLICATION_PROBLEM_XML

          public static final ContentType APPLICATION_PROBLEM_XML

      Public constant media type for `application/problem+xml`.
      Since:
        5.1
      See Also:
        Problem Details for HTTP APIs, 6.2. application/problem+xml

    - #### APPLICATION_RSS_XML

          public static final ContentType APPLICATION_RSS_XML

      Public constant media type for `application/rss+xml`.
      Since:
        5.1

    - #### IMAGE_BMP

          public static final ContentType IMAGE_BMP

    - #### IMAGE_GIF

          public static final ContentType IMAGE_GIF

    - #### IMAGE_JPEG

          public static final ContentType IMAGE_JPEG

    - #### IMAGE_PNG

          public static final ContentType IMAGE_PNG

    - #### IMAGE_SVG

          public static final ContentType IMAGE_SVG

    - #### IMAGE_TIFF

          public static final ContentType IMAGE_TIFF

    - #### IMAGE_WEBP

          public static final ContentType IMAGE_WEBP

    - #### MULTIPART_FORM_DATA

          public static final ContentType MULTIPART_FORM_DATA

    - #### MULTIPART_MIXED

          public static final ContentType MULTIPART_MIXED

      Public constant media type for `multipart/mixed`.
      Since:
        5.1

    - #### MULTIPART_RELATED

          public static final ContentType MULTIPART_RELATED

      Public constant media type for `multipart/related`.
      Since:
        5.1

    - #### TEXT_HTML

          public static final ContentType TEXT_HTML

    - #### TEXT_MARKDOWN

          public static final ContentType TEXT_MARKDOWN

      Public constant media type for `text/markdown`.
      Since:
        5.1

    - #### TEXT_PLAIN

          public static final ContentType TEXT_PLAIN

    - #### TEXT_XML

          public static final ContentType TEXT_XML

    - #### TEXT_EVENT_STREAM

          public static final ContentType TEXT_EVENT_STREAM

      Public constant media type for `text/event-stream`.
      Since:
        5.1
      See Also:
        Server-Sent Events W3C recommendation

    - #### WILDCARD

          public static final ContentType WILDCARD

    - #### DEFAULT_TEXT

          public static final ContentType DEFAULT_TEXT

    - #### DEFAULT_BINARY

          public static final ContentType DEFAULT_BINARY

  - ### Method Detail

    - #### getMimeType

          public String getMimeType()

    - #### getCharset

          public Charset getCharset()

    - #### getParameter

          public String getParameter(String name)

      Since:
        4.3

    - #### toString

          public String toString()

      Generates textual representation of this content type which can be used as the value of a `Content-Type` header.
      Overrides:
        `toString` in class `Object`

    - #### create

          public static ContentType create(String mimeType,
                           Charset charset)

      Creates a new instance of `ContentType`.
      Parameters:
        `mimeType` - MIME type. It may not be `null` or empty. It may not contain characters `<">, <;>, <,>` reserved by the HTTP specification.
        `charset` - charset.
      Returns:
        content type

    - #### create

          public static ContentType create(String mimeType)

      Creates a new instance of `ContentType` without a charset.
      Parameters:
        `mimeType` - MIME type. It may not be `null` or empty. It may not contain characters `<">, <;>, <,>` reserved by the HTTP specification.
      Returns:
        content type

    - #### create

          public static ContentType create(String mimeType,
                           String charset)
                                    throws UnsupportedCharsetException

      Creates a new instance of `ContentType`.
      Parameters:
        `mimeType` - MIME type. It may not be `null` or empty. It may not contain characters `<">, <;>, <,>` reserved by the HTTP specification.
        `charset` - charset. It may not contain characters `<">, <;>, <,>` reserved by the HTTP specification. This parameter is optional.
      Returns:
        content type
      Throws:
        `UnsupportedCharsetException` - Thrown when the named charset is not available in this instance of the Java virtual machine

    - #### create

          public static ContentType create(String mimeType,
                           NameValuePair... params)
                                    throws UnsupportedCharsetException

      Creates a new instance of `ContentType` with the given parameters.
      Parameters:
        `mimeType` - MIME type. It may not be `null` or empty. It may not contain characters `<">, <;>, <,>` reserved by the HTTP specification.
        `params` - parameters.
      Returns:
        content type
      Throws:
        `UnsupportedCharsetException`
      Since:
        4.4

    - #### parse

          public static ContentType parse(CharSequence s)
                                   throws UnsupportedCharsetException

      Parses textual representation of `Content-Type` value.
      Parameters:
        `s` - text
      Returns:
        content type `Content-Type` value.
      Throws:
        `UnsupportedCharsetException` - Thrown when the named charset is not available in this instance of the Java virtual machine

    - #### parseLenient

          public static ContentType parseLenient(CharSequence s)
                                          throws UnsupportedCharsetException

      Parses textual representation of `Content-Type` value ignoring invalid charsets.
      Parameters:
        `s` - text
      Returns:
        content type `Content-Type` value.
      Throws:
        `UnsupportedCharsetException` - Thrown when the named charset is not available in this instance of the Java virtual machine

    - #### getByMimeType

          @Deprecated
          public static ContentType getByMimeType(String mimeType)

      Deprecated. Do not use. This method was made public by mistake.
      Returns `Content-Type` for the given MIME type.
      Parameters:
        `mimeType` - MIME type
      Returns:
        content type or `null` if not known.
      Since:
        4.5

    - #### withCharset

          public ContentType withCharset(Charset charset)

      Creates a new instance with this MIME type and the given Charset.
      Parameters:
        `charset` - charset
      Returns:
        a new instance with this MIME type and the given Charset.
      Since:
        4.3

    - #### withCharset

          public ContentType withCharset(String charset)

      Creates a new instance with this MIME type and the given Charset name.
      Parameters:
        `charset` - name
      Returns:
        a new instance with this MIME type and the given Charset name.
      Throws:
        `UnsupportedCharsetException` - Thrown when the named charset is not available in this instance of the Java virtual machine
      Since:
        4.3

    - #### withParameters

          public ContentType withParameters(NameValuePair... params)
                                     throws UnsupportedCharsetException

      Creates a new instance with this MIME type and the given parameters.
      Parameters:
        `params` -
      Returns:
        a new instance with this MIME type and the given parameters.
      Throws:
        `UnsupportedCharsetException`
      Since:
        4.4

    - #### isSameMimeType

          public boolean isSameMimeType(ContentType contentType)

Copyright © 2005–2022 The Apache Software Foundation. All rights reserved.