{"record":{"id":"4370ffd92eff7e53","repo":"nicolargo/glances","slug":"invalid-json-body","errorCode":null,"errorMessage":"Invalid JSON body","messagePattern":"Invalid JSON body","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"warning","filePath":"glances/outputs/glances_restful_api.py","lineNumber":827,"sourceCode":"        # Check if JWT is available\n        if self._jwt_handler is None or not self._jwt_handler.is_available:\n            raise HTTPException(\n                status.HTTP_501_NOT_IMPLEMENTED,\n                \"JWT authentication is not available. Install python-jose or check configuration.\",\n            )\n\n        # Check if password authentication is enabled\n        if self._password is None:\n            raise HTTPException(\n                status.HTTP_501_NOT_IMPLEMENTED,\n                \"Password authentication is not enabled. Start Glances with --password option.\",\n            )\n\n        # Parse request body\n        try:\n            body = await request.json()\n        except Exception:\n            raise HTTPException(status.HTTP_400_BAD_REQUEST, \"Invalid JSON body\")\n\n        username = body.get('username')\n        password = body.get('password')\n\n        if not username or not password:\n            raise HTTPException(\n                status.HTTP_400_BAD_REQUEST,\n                \"Missing username or password in request body\",\n            )\n\n        # Validate credentials\n        if username != self.args.username:\n            raise HTTPException(\n                status.HTTP_401_UNAUTHORIZED,\n                \"Incorrect authentication\",\n                {\"WWW-Authenticate\": \"Bearer\"},\n            )\n","sourceCodeStart":809,"sourceCodeEnd":845,"githubUrl":"https://github.com/nicolargo/glances/blob/a240d8dfb3105a38b5964357ec21768594b0e83e/glances/outputs/glances_restful_api.py#L809-L845","documentation":"/api/4/token expects a JSON body; if request.json() raises (malformed JSON, wrong Content-Type handling, empty body), Glances returns 400 'Invalid JSON body'. It's a request-shape validation error, not an auth failure.","triggerScenarios":"POSTing form-encoded data without JSON headers, sending truncated JSON, or an empty body; curl -d '{bad json'.","commonSituations":"Clients sending application/x-www-form-urlencoded (the OAuth-style default in many HTTP tools); proxies stripping bodies; copy-paste shell quoting mistakes breaking the JSON.","solutions":["Send a valid JSON body with the JSON content type: curl -H 'Content-Type: application/json' -d '{\"username\":\"u\",\"password\":\"p\"}'.","Validate your payload with a JSON linter/jq before sending.","Check that no proxy or middleware rewrites the body."],"exampleFix":"# before\ncurl -X POST http://host:61208/api/4/token -d 'username=u&password=p'\n# 400 Invalid JSON body\n\n# after\ncurl -X POST http://host:61208/api/4/token -H 'Content-Type: application/json' -d '{\"username\":\"u\",\"password\":\"p\"}'","handlingStrategy":"validation","validationCode":"import json\njson.dumps({'username': user, 'password': pwd})  # validate payload before sending\nrequests.post(url, json=payload)  # json= sets Content-Type automatically","typeGuard":null,"tryCatchPattern":"r = requests.post(url, json=payload)\nif r.status_code == 400 and 'Invalid JSON' in r.text:\n    payload = json.dumps(payload)  # re-serialize and retry","preventionTips":["Use requests' json= parameter (or set Content-Type: application/json).","Avoid -d with form-encoded strings against JSON endpoints.","Validate payloads with jq/JSON linters in scripts."],"tags":["rest-api","json","http-400","request-validation"],"backgroundTag":"invalid-json-body","analyzedSha":"a240d8dfb3105a38b5964357ec21768594b0e83e","analyzedAt":"2026-08-27T19:15:19.178Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}