Content Type and the Bytes in an HTTP Message

Content-Type describes the format of the associated representation. It helps a recipient choose how to interpret the bytes.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace an unexpected format
  2. Keep decoding and parsing clear
  3. Interpret declared media type before parsing bytes
  4. Distinguish response type from requested preferences

Trace an unexpected format

If a client expects JSON and receives an HTML error page, trying to parse the content as JSON gives a format failure. Check the status and declared representation before choosing a parser. Structural validation gives a later layer.

Keep decoding and parsing clear

A format can declare how text becomes bytes and how characters describe structured data. Those are separate steps. Escaping versus encoding explains one practical consequence.[1]

Interpret declared media type before parsing bytes

A response whose fields declare application/json should carry a JSON representation when it has content under that contract. An HTML error page can begin with different bytes even if a frontend expected JSON. Inspect status and declared type before interpreting the response. See HTTP response anatomy.

Expected header: Content-Type: application/json
Example body: {"ready":true}
If body is a markup error page instead, JSON decoding fails

Distinguish response type from requested preferences

Accept expresses media preferences, whereas Content-Type describes the representation carried. Test a supported preference and an actual error response. Keep decoding and schema validation as separate checks.

References

  1. IETF, RFC 9110: HTTP Semantics ↩