Speakeasy Logo
Skip to Content
OpenAPI HubContent & Media

Content and Media Types in OpenAPI

In OpenAPI 3.1, the content keyword indicates the media types required in request bodies or returned by responses. Media types are often referred to as content types or MIME types, but we’ll use media types in this document.

Media types in OpenAPI inform the client how to interpret data received from the server, and which data types the server expects from the client.

Common examples of media types include:

  • application/json for JSON objects.
  • text/plain for plain text.
  • image/png for PNG image files.
  • application/xml for XML files.
  • multipart/form-data for form data that can include files.

Content Map in OpenAPI

The content object is a map of key-value pairs.

Each key in the map is a media or MIME type  like application/json, text/plain, or image/png.

The value associated with each key is a Media Type Object that describes the structure and other relevant details for its corresponding media type.

Media type keys can include wildcards indicating a range of media types they cover. For example, application/* would match application/json, application/xml, and so on. It can be explicitly defined to match only a single media type, for example, application/json; charset=utf-8.

Where both a wildcard and a specific media type are defined, the specific media type definition takes precedence.

The example below shows a content map with four media types:

In this example, the server expects one of the following types:

  • A JSON object representing a drink.
  • Any image file in binary format.
  • A CSV file representing a drink.
  • Any text file.

Content Negotiation

When the client sends a request to the server, it includes a Content-Type HTTP header in the request, indicating to the server how to interpret the data in the body of the request.

Likewise, the server includes a Content-Type HTTP header in its response, which the client should use to interpret the data in the response.

The client may also include an Accept HTTP header in a request, indicating to the server which content types the client can handle. The server should then send a response with a Content-Type header that matches one of the accepted types. This exchange is known as content negotiation .

The diagram below illustrates the headers sent by the client and server during content negotiation:

Note that the request and response content types do not need to match. For example, in the diagram above, the client sends a request as CSV but expects JSON or XML in response.

Media Type Object

A Media Type Object describes the request or response for a media type, with optional examples and extensions.

Media Type Object Fields

Field
Required
Description
A schema that describes the request or response content.
Required
Description
Optional examples of the media type. These examples override any examples from the Schema Object in the
field. Mutually exclusive with the
field.
Type
Any
Required
Description
An optional example of the media type. This example overrides any examples from the Schema Object in the
field. Mutually exclusive with the
field. Deprecated in OpenAPI 3.1 in favor of
.
Type
Map[string, Encoding Object]
Required
Description
An optional map of Encoding Objects. Each Encoding Object's key should match one of the properties from the Schema Object in the
field. Only applies to Request Body Objects when the media type is
or
.
Required
Description
Any number of extension fields as required by tooling and vendors.

Media Type Examples

The examples below illustrate the use of the content object with different media types.

JSON Media Type

The example below shows a content object with a JSON media type:

In this example, the server expects a JSON object representing a drink. The examples field provides an Example Object of the expected JSON object.

The curl command below sends a request to the server with a JSON object in the body:

Image Media Type

The example below shows a content object with an image media type:

In this example, the server expects an image file in binary format.

The curl command below sends a request to the server with an image file in the body:

Text Media Type

The example below shows a content object with a text media type:

In this example, the server expects a plain text file.

The curl command below sends a request to the server with a text file in the body:

CSV Media Type

The example below shows a content object with a CSV media type:

In this example, the server expects a CSV file representing a drink.

The curl command below sends a request to the server with a CSV file in the body:

Multipart Form Data

The example below shows a content object with a multipart form data media type:

In this example, the server expects a form data request with a photo of the drink, the recipe for the drink, and the name of the drink. The encoding field provides additional information about each part, such as the content type, headers, and whether reserved characters are allowed.

The curl command below sends a request to the server with a photo file, a recipe file, and the name of the drink in the body:

OpenAPI Content Best Practices

When designing APIs with OpenAPI, consider the following best practices for content and media types:

  • Where possible, use the most specific media type for your content. For example, prefer application/json over application/* if your content is JSON.
  • When using OpenAPI 3.1, provide at least one example for each media type using the examples keyword to help clients understand the expected content and enrich the API documentation.

Last updated on