generation API JSON content type: separate request format from error parsing
·
Content type is part of an HTTP contract, but it does not prove that the body is valid JSON. A generation client should declare what it sends and inspect what it receives. A reverse proxy can return an HTML error page before the API handler runs, so assuming that every response supports response.json() hides the original failure behind a parsing exception.
Send a serialized object explicitly
For a generation POST, use the JSON media type and serialize the body once. Do not pass a JavaScript object directly as Fetch’s body or confuse a form submission with a JSON document. The example constructs a request only; executing it creates paid work and should remain behind your application’s authorization boundary.
export function imageRequestOptions(key, prompt) {
return {
method: 'POST',
headers: {
Authorization: `Bearer ${key}`,
'Content-Type': 'application/json',
Accept: 'application/json',
},
redirect: 'error',
body: JSON.stringify({ model: 'nano-banana', prompt, n: 1 }),
};
}
The public image route calls its request JSON parser and produces INVALID_JSON when parsing fails. It does not define a comprehensive global request-size promise or a strict media-type rejection policy in that handler. If your own proxy requires JSON and returns 415 for unsupported formats, describe that as your application’s contract.
Both generation routes also require a non-null JSON object at the top level.
null, arrays, strings, numbers, and booleans can be valid JSON syntax, but they
are not generation request objects. They return HTTP 400 with INVALID_PARAM
and param: "body", before generation job reservation or provider submission.
Malformed JSON syntax still returns INVALID_JSON. An object such as {}
passes the structural check but fails the provider’s required prompt validation;
do not confuse these different errors.
Authentication and quota controls still run before body handling. This object check does not introduce a global 415 policy, validate all object fields, or establish whether a different service’s unsuccessful request created work.
Normalize media types without discarding evidence
Read the response Content-Type header, separate parameters such as charset, and retain the HTTP status. A missing or unexpected header deserves a controlled diagnostic branch. A response claiming JSON can still be malformed, so handle parsing failure independently from media-type classification.
For a client dedicated to this API, accepting application/json is sufficient for the documented JSON responses. If you build a more general parser that supports structured suffix types such as application/problem+json, specify that policy deliberately and do not assume those bodies have Genace’s error envelope.
Preserve the generation lifecycle
A valid JSON response can describe acceptance, request rejection, or a job snapshot. After HTTP 202, save the top-level job_id. After a submission failure with HTTP 502 and UPSTREAM_SUBMISSION_FAILED, inspect error.job_id if present. A GET snapshot uses id and may report failed even when HTTP status is successful.
Do not use the response media type to decide whether another generation is needed. HTML from an intermediary may mean the request never reached the handler, but it can also conceal a response after work began. Keep an uncertain submission branch whenever acceptance cannot be established from the available evidence.
Test format mistakes locally
Create fixtures for valid accepted JSON, malformed JSON with a JSON header, HTML with HTTP 502, and an empty response. Test incoming requests with a supported media type, an unsupported form type, and invalid syntax against your own proxy. Assert the resulting error preserves status and never includes the Authorization header.
Also check that a malformed successful body does not produce an accepted toast with an undefined job ID. The absence of a usable identifier is a recovery problem, not permission to repeat a paid POST automatically.
Include top-level null and arrays separately from broken JSON syntax in your
request fixtures. Verify neither creates a job or reserves credits. Test a
valid object after those failures to ensure default model selection, alias
precedence, and a seed of zero remain intact. Inject a reservation write failure
in an isolated database and confirm the deduction rolls back before any upstream
submission; this is separate from handling an uncertain upstream response.
Related guides explain single body consumption, error schema validation, and input validation.
Continue with code and integration
Where these facts come from
- Editorial keyword hypothesis; search volume not measured
- https://www.rfc-editor.org/rfc/rfc9110.html#name-content-type
- codebase: src/app/api/v1/images/generations/route.ts
- codebase: src/app/api/v1/video/generations/route.ts
- codebase: src/ai/api/errors.ts