liveBook · Manning

Base64

As explained in The Design of Web APIs, Second Edition

Base64 is a binary-to-text encoding that represents arbitrary bytes using a restricted set of ASCII characters.

Base64 encodes binary bytes as text so an API can embed files in JSON. It simplifies single-call uploads, but increases content size by 33%; compression reduces the practical increase for some files. section 14.3.2

Single-call account application

A POST /account-applications request can include applicant data and a documents array. Each document identifies an identity or residence file and carries its Base64-encoded content. Multipart/form-data is usually smaller, but Base64 remains useful when JSON-only integration is simpler or required. section 14.3.2

Describe embedded files explicitly:

  • Set JSON Schema contentMediaType to the file type, such as image/png. section 14.3.5
  • Set contentEncoding to base64 for the encoded string. section 14.3.5
  • Prefer multipart/form-data for larger or mixed uploads when consumers and gateways support it. section 14.3.2
Listing 14.3 Embedding a file in JSON
...
components:
  schemas:
    Account:
      properties:
        ...
        icon:
          type: string
          contentMediaType: image/png    #1
          contentEncoding: base64               #2
        ...

OpenAPI 3.1 example declaring a PNG property as Base64-encoded content. section 14.3.5

How The Design of Web APIs, Second Edition covers it

How deep it goes
Defines encoding trade-offs and OpenAPI declarations, while deferring detailed binary-transfer optimization to other mechanisms.
Examples
Uses account applications, identity and residence documents, and PNG or SVG image properties.
What you can do after
Apply it via JSON Schema and OpenAPI declarations when designing JSON-based file uploads.
How it connects
Connects JSON Schema, OpenAPI, multipart/form-data, HTTP compression, and file upload design.

Also explained in

Compare how 7 books explain Base64 →

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.