Skip to content

Media types

Sebastien Dubois edited this page Dec 10, 2016 · 3 revisions

About

Media types (also known as MIME types or content types) play an important role for HTTP and RESTful APIs.

Media types provide a way for clients to specify the data format they expect and for the server to state the data format that they return.

A media type is composed of

  • a type
  • a subtype
  • optional parameters.

Some examples:

  • text/html
  • application/json
  • application/xml
  • application/x-www-form-urlencoded

All media types are registered with the IANA; the official list is there: http://www.iana.org/assignments/media-types/media-types.xhtml

Most of the time with RESTful APIs, you'll use the "application/json" media type as that's the standard IANA registered media type for JSON.

Vendor-specific media types

Media types use the subtype prefix "vnd" to indicate that they are owned or controlled by a "vendor". Vendor-specific media types convey a clear description of a message's content to the programs that understand their meaning.

You MAY consider using vendor-specific media types to convey more clearly the type of content that is associated with a request or response (i.e., give not only information about the content but also about its semantics). Although, we do not recommend that (yet).

Example with a request:

POST .../v1/employees
Content-Type: vnd.mycompany-employee-v1+json
{
    "firstName": "foo",
    "lastName": "bar",
    ...
}

If the application knows that content type, it can parse it much more efficiently.

Rules

The media type MUST be specified in...

  • Requests
    • Accept: <media type(s)> (http header)
      • expected response content type
  • Responses
    • Content-Type: (http header)

In addition your API...

  • MUST send a 406 (Not Acceptable) error code if it cannot generate any of the client's preferred media types.
  • MUST send a 415 (Unsupported Media Type) error code if it cannot process the client's supplied media type.
  • SHOULD try to leverage vendor-specific media types when possible
    • it helps convey not only data format information, but semantic information about the data

History

REST

REST

REST API Design Goals

REST Constraints

REST Resources

REST Resources Design Workflow

REST Resources Naming

REST Resources URIs

REST Resources Parameters

REST Resources Single items and collections

REST Resources Relations

REST Resources Many to many Relations

REST Resources Relations expansion

REST Resources Actions

REST API Versioning

REST API Documentation

HTTP

HTTP Methods

HTTP Status Codes

HTTP Status Codes Success (2xx)

HTTP Status Codes Redirection (3xx)

HTTP Status Codes Client Error (4xx)

HTTP Status Codes Server Error (5xx)

Media types

CRUD Operations

CRUD About

CRUD Create Single item

CRUD Retrieve Single item

CRUD Retrieve Collection

CRUD Update Single item

CRUD Delete Single item

Pagination

Pagination About

Pagination Rules and metadata

Pagination Example

Pagination Out of range/bounds

Filtering

Filtering About

Filtering Using styles

Filtering Using includes

Filtering Using excludes

Sorting

Sorting About

Sorting Metadata

Sorting Example

Searching

Searching About

Searching Local search

Searching Scoped search

Searching Global search

Searching Advanced search

Searching Wildcards

Searching Formatting

Long-running operations

Long-running Operations About

Long-running Operations Flow

Long-running Operations Rules

Long-running Operations Example

Concurrency control

Concurrency About

Concurrency Headers to use

Concurrency vs Delete operation

Concurrency vs Pagination

Caching and conditional requests

Caching and conditional requests About

Caching and conditional requests Rules

Caching and conditional requests HTTP headers

Conditional requests

Cache control

Error handling

Error handling About

Error handling Expectations

Error handling Status codes

Error handling Error details

Error handling Example with a single error

Error handling Example with multiple errors

Error handling Example with parameters

Error handling Example with additional metadata

Error handling Warnings

Compression

Compression About

Bulk operations

Bulk operations About

Bulk operations Types

Bulk operations Atomic

Bulk operations Non-atomic

Bulk operations Asynchronous

Bulk operations HTTP status codes

Bulk operations Resources naming convention

Bulk operations Errors

Bulk operations Creation example

Bulk operations Update example

Bulk operations Create and update example

File upload

File upload About

File upload File sizes

File upload Simple file upload

File upload Simple file upload example

File upload Complex file upload

File upload Complex file upload example

Security recommendations

REST Security General recommendations

REST Security Transport layer

REST Security Error handling

REST Security Insecure direct object references

REST Security CORS

REST Security Updates and consistency

REST Security API keys

Miscellaneous

Data formats

Internationalization

Rate limiting

Null values

Dates and times

Redirections

References

Clone this wiki locally