{"record":{"id":"d401d649f6e48e7e","repo":"BoundaryML/baml","slug":"failed-deserializing-http-body-as-json-e","errorCode":null,"errorMessage":"Failed deserializing HTTP body as JSON:\n{e:?}","messagePattern":"Failed deserializing HTTP body as JSON:\n(.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"engine/language_client_ruby/ext/ruby_ffi/src/types/request.rs","lineNumber":91,"sourceCode":"        for byte in rb_self.inner.raw() {\n            array.push(*byte)?;\n        }\n\n        Ok(array)\n    }\n\n    pub fn text(ruby: &Ruby, rb_self: &Self) -> Result<String> {\n        rb_self.inner.text().map(String::from).map_err(|e| {\n            Error::new(\n                ruby.exception_runtime_error(),\n                format!(\"Failed to get text from HTTP body:\\n{e:?}\"),\n            )\n        })\n    }\n\n    pub fn json(ruby: &Ruby, rb_self: &Self) -> Result<magnus::Value> {\n        serde_magnus::serialize(&rb_self.inner.json().map_err(|e| {\n            Error::new(\n                ruby.exception_runtime_error(),\n                format!(\"Failed deserializing HTTP body as JSON:\\n{e:?}\"),\n            )\n        })?)\n    }\n\n    pub fn define_in_ruby(module: &RModule) -> Result<()> {\n        let cls = module.define_class(\"HTTPBody\", class::object())?;\n\n        cls.define_method(\"raw\", method!(HTTPBody::raw, 0))?;\n        cls.define_method(\"text\", method!(HTTPBody::text, 0))?;\n        cls.define_method(\"json\", method!(HTTPBody::json, 0))?;\n\n        Ok(())\n    }\n}\n","sourceCodeStart":73,"sourceCodeEnd":108,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_ruby/ext/ruby_ffi/src/types/request.rs#L73-L108","documentation":"HTTPBody#json reads the body and parses it as JSON, then converts it to a Ruby value. If the body is not valid JSON (or cannot be read), a Ruby RuntimeError 'Failed deserializing HTTP body as JSON' with the debug error is raised.","triggerScenarios":"Calling json on an HTTPBody containing non-JSON content: empty body, HTML error pages, plain-text errors, or an already-consumed body.","commonSituations":"Hitting an upstream gateway/proxy error page (502 HTML), an endpoint that returns plain text, or parsing the same body after a prior text() call.","solutions":["Check the response status and content-type before calling json.","Read the body once; store the text and parse manually if you need both forms.","Inspect the debug error in the message to distinguish read failure vs JSON parse failure."],"exampleFix":"// before\ndata = response.body.json # fails on HTML error page\n// after\nbody = response.body\nif response.status == 200 && body.text.strip.start_with?('{', '[')\n  data = JSON.parse(body.text)\nelse\n  data = nil\nend","handlingStrategy":"validation","validationCode":"raw = body.text rescue nil\nparsed = raw && raw.strip.start_with?('{', '[') ? JSON.parse(raw) : nil","typeGuard":null,"tryCatchPattern":"begin\n  data = body.json\nrescue RuntimeError => e\n  data = nil\nend","preventionTips":["Check HTTP status/content-type before parsing JSON.","Don't reuse a body already consumed by text()."],"tags":["ruby","ffi","http-body","json"],"backgroundTag":"invalid-json-response","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}