AC-010 Contract caught CS-RESP-008

The body is not valid JSON, or is empty

The response does not match the contract

How to run this card's commands yourself

1. The problem

In one sentence

The Content-Type says JSON, but the body cannot be parsed as JSON, or the body is empty where the spec declares one.

A story

A client calls GET /cases/resp-008-invalid-json. The spec says 200 with a JSON widget. The server answers 200, content-type: application/json, and the body {not json. A proxy cut the real body, or a handler wrote half a template. The client’s JSON.parse throws, and the app shows a generic error.

Why it hurts

A body that is not JSON cannot be compared with anything: not one field is available. The status says success, so retry and error logic that look only at the status do nothing.

  • For developers: the body is often an HTML error page from a proxy, or a partial write. Reading it with curl -i shows the cause in seconds.
  • For managers: the service looks up (status 200) while customers see errors. Monitoring that checks only status codes will not alert.
  • For DevOps: check the body, not just the status, in health checks. An empty 200 is a classic result of a crashed worker behind a load balancer.

Example

The spec fragment declares a JSON body:

responses:
  "200":
    description: The declared response
    content:
      application/json:
        schema:
          $ref: "#/components/schemas/Widget"

The response:

HTTP/1.1 200 OK
content-type: application/json

{not json

A second fixture, resp-008b-empty-body, answers 200 with content-length: 0 where the spec declares the same Widget schema.

How to detect it without clrsig

  • curl -s URL | jq . prints a parse error for invalid JSON, and nothing for an empty body.
  • curl -i URL shows the real body and the content-length.

How to fix it

There are two usual causes:

  1. The server is wrong (more likely). A layer in front of it (proxy, load balancer) replaced the body, or the handler failed half-way. Make the server send valid JSON, or the right error status.
  2. The spec is wrong. The operation really returns no body, or another media type. Then declare that (for example 204, or no content).

To tell which: look at the real bytes with curl -i. Never silence the check.

AC-002 The Content-Type is not the one the spec promised, AC-016 Malformed JSON that parsers read differently, AC-017 The body is cut off, or the connection drops.

Words used on this page are explained once in the glossary.

2. How clrsig helps

Check

CS-RESP-008 Body is not valid JSON, or is empty (findings invalid-body and empty-body). State: caught. Severity today: failure (exit code 1). clrsig parses the body as strict JSON.

Command

--paths limits the run to this one GET, so nothing is written:

clrsig conform specs/contract-fail.yaml \
  -b http://localhost:8080/contract-fail \
  --env clrsig/environments/contract-fail.yaml \
  --paths /cases/resp-008-invalid-json --explain

Exit code: 0 clean, 1 a finding, 2 the spec or the setup is broken. Here it is 1.

What you see

Output from clrsig v0.21.0, recorded on 2026-11-30. The lines for the 36 operations that --paths skipped are left out by grep -v '^ SKIP'; no line is edited.

contract-fail API  1.0.0
Base URL: http://localhost:8080/contract-fail
Limited by --paths /cases/resp-008-invalid-json

  FAIL  GET     /cases/resp-008-invalid-json  200  19ms
         ✗ GET /cases/resp-008-invalid-json 200: the response body is not valid JSON (invalid character 'n' looking for beginning of object key string)
         → [CS-RESP-008] The response body is not valid JSON. Why: The spec declares a JSON body for response 200, but what arrived cannot be parsed, so no field was compared. Where: response body (spec: GET /cases/resp-008-invalid-json → response 200 → body schema). Fix: Make the server send valid JSON (check for a truncated, HTML or plain-text error page), or correct the declared media type in the spec.

FAILED  37 operations  ·  0 passed  ·  1 failed  ·  36 skipped  20ms

For the empty body (--paths /cases/resp-008b-empty-body), the same run says: the response body is empty, but the spec declares a schema for it, with the fix “Return the body from the server, or declare response 200 without content in the spec.”

How to read it: the parser’s own message (invalid character 'n' ...) says where parsing stopped. The → line points at the likely causes: a truncated, HTML or plain-text error page.

Where to look

  • Desktop app: Check Conformance lists the failure. Help, What We Check lists CS-RESP-008.
  • Reports: with --format json, the finding carries "checkId": "CS-RESP-008" and the kind invalid-body or empty-body.
  • GitHub Action: it runs the same command.
  • Scenario: a schema assertion fails too; an empty body fails with “response body is empty”.

How clrsig helps you fix it

It says whether the body is invalid or empty, shows the parser’s message, and suggests looking for an error page. It cannot show you the body; use curl -i.

Limits

  • Strict JSON only: text after the value, bytes that are not UTF-8, and a syntax error all fail.
  • A body of another type (XML, CSV) is not an error here; clrsig does not read it as JSON.

More ways

Nothing yet.

3. Video