invalid_json

The message text is shown in the example below.

HTTP status400
typeinvalid_request_error
Retry?No, not unchanged
StatusIn the public catalog (error-envelope.ts)
Extra fieldsnone

When it happens

The body could not be parsed as JSON: a trailing comma, single quotes, an unescaped newline inside a string, or a body sent as form data.

Example

{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_json",
    "message": "The request body is not valid JSON.",
    "param": null,
    "request_id": "req_6f1c2a9e0b7d4c3a8e5f1b2d"
  }
}

How to fix it

  • Build the body with your language’s JSON encoder instead of string concatenation.
  • In shell scripts, put the body in a file and send it with -d @body.json, which avoids quoting mistakes.
  • Send Content-Type: application/json.

Detect it in code

err = resp.json().get("error", {})
if resp.status_code == 400 and err.get("code") == "invalid_json":
    log.warning("%s (%s)", err["message"], err["request_id"])  # fix the request; do not retry unchanged
Was this page helpful?