liveBook · Manning

MIME type

As explained in The Design of Web APIs, Second Edition

A media type is a standardized identifier describing the format and general nature of data exchanged in an HTTP message.

A MIME type, also called a media type, identifies the format and general nature of data in an HTTP request or response. For example, application/json indicates that a body contains JSON. In OpenAPI, declaring the media type makes request and response formats visible to documentation tools and provides the place to attach a schema. section 6.6.2 section 9.7.1

Choosing JSON or XML

A transfer API can accept XML when a banking system sends Content-Type: application/xml, while returning XML or JSON according to the consumer’s Accept header. Request and response formats do not need to match. A transaction endpoint might return JSON by default but provide CSV when a spreadsheet-oriented consumer requests Accept: text/csv. section 9.7.1

Use media types to make format choices explicit and useful to consumers:

  • Declare application/json with a schema for structured request or response data. section 6.6.2
  • Support only formats that fit the operation, while keeping format behavior consistent across related operations. section 9.7.1
  • Offer alternate representations such as application/hal+json or application/vnd.siren+json through content negotiation. section 11.4.4
  • Describe raw files with types such as application/pdf, image/png, image/jpeg, image/*, or application/octet-stream. section 14.3.4
Listing 14.1 Sending a file in a request body
paths:
  /files:
    post:
      requestBody:
        content:
          application/pdf: {}    #1
          image/png: {}          #1
          image/jpeg: {}         #1
      responses:
        "201":                     #2
          description: File uploaded

This OpenAPI listing shows how an upload operation accepts PDF, PNG, and JPEG bodies by declaring each media type under content. section 14.3.4

How The Design of Web APIs, Second Edition covers it

How deep it goes
Explains declarations, headers, negotiation, alternate representations, and file formats, but defers detailed schema definitions and unsupported-format handling.
Examples
Uses JSON product requests, CSV transaction responses, XML transfers, hypermedia representations, and PDF, PNG, and JPEG file uploads.
What you can do after
Designs and documents API payload formats, then negotiates suitable representations through Accept and Content-Type headers.
How it connects
Connects media types to HTTP, OpenAPI, content negotiation, Accept, Content-Type, JSON, XML, CSV, hypermedia, and multipart/form-data.

Also explained in

Compare how 2 books explain MIME type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Design of Web APIs, Second Edition. The section links point to the parts of the book it draws on.