{"record":{"id":"1bb8690d3896aef8","repo":"apache/iceberg","slug":"failed-to-encode-request-body-s","errorCode":null,"errorMessage":"Failed to encode request body: %s","messagePattern":"Failed to encode request body: (.+?)","errorType":"exception","errorClass":"RESTException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/apache/iceberg/rest/HTTPRequest.java","lineNumber":109,"sourceCode":"\n  /** Returns the raw, unencoded request body. */\n  @Nullable\n  @Value.Redacted\n  Object body();\n\n  /** Returns the encoded request body as a string. */\n  @Value.Lazy\n  @Nullable\n  @Value.Redacted\n  default String encodedBody() {\n    Object body = body();\n    if (body instanceof Map) {\n      return RESTUtil.encodeFormData((Map<?, ?>) body);\n    } else if (body != null) {\n      try {\n        return mapper().writeValueAsString(body);\n      } catch (JsonProcessingException e) {\n        throw new RESTException(e, \"Failed to encode request body: %s\", body);\n      }\n    }\n    return null;\n  }\n\n  /**\n   * Returns the {@link ObjectMapper} to use for encoding the request body. The default is {@link\n   * RESTObjectMapper#mapper()}.\n   */\n  @Value.Default\n  default ObjectMapper mapper() {\n    return RESTObjectMapper.mapper();\n  }\n\n  @Value.Check\n  default void check() {\n    if (path().startsWith(\"/\")) {\n      throw new RESTException(","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/apache/iceberg/blob/86d9c8fc543e7c56c9f624eb725f76c9baff9570/core/src/main/java/org/apache/iceberg/rest/HTTPRequest.java#L91-L127","documentation":"HTTPRequest.encodedBody serializes the request body: Maps go through RESTUtil.encodeFormData, other non-null objects through the shared Jackson ObjectMapper. If Jackson throws JsonProcessingException, the client wraps it in a RESTException with this message. It means the request payload could not be serialized to JSON before sending.","triggerScenarios":"Calling HTTPRequest.post(...).withBody(obj) where obj is not JSON-serializable — e.g. an object with self-references (infinite recursion), a type Jackson has no serializer for, or a POJO with failing getters that throw during serialization.","commonSituations":"Passing raw domain objects instead of Iceberg REST request types (like RegisterTableRequest); non-serializable custom types (InputStream, lambdas) in the body; getters that throw IllegalStateException during property access.","solutions":["Send Iceberg's REST request types (e.g. RegisterTableRequest, UpdateNamespacePropertiesRequest) instead of ad-hoc objects.","Inspect the chained JsonProcessingException cause to find the offending property and fix or exclude it (@JsonIgnore-style exclusion in your own mapper-compatible type).","Ensure body getters never throw and the object has no cyclic references.","If sending a form, pass a Map so it is encoded as form data instead of JSON."],"exampleFix":"// before\nrequest.withBody(myDomainObjectWithCycles);\n// after\nrequest.withBody(RegisterTableRequest.builder()\n    .name(tableName)\n    .location(location)\n    .build());","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean jsonSerializable(Object body) {\n  try {\n    RESTObjectMapper.mapper().writeValueAsString(body);\n    return true;\n  } catch (JsonProcessingException e) {\n    return false;\n  }\n}","tryCatchPattern":"try {\n  request.withBody(body);\n} catch (RESTException e) {\n  if (e.getMessage().startsWith(\"Failed to encode request body\")) {\n    // inspect e.getCause() for the offending property; use a REST request type\n  }\n  throw e;\n}","preventionTips":["Use Iceberg REST request types as bodies instead of arbitrary domain objects.","Ensure body getters are side-effect free and classes are free of cycles.","Pre-test custom body types by serializing them with the shared mapper."],"tags":["serialization","json","jackson","rest-catalog"],"backgroundTag":"json-marshal-failed","analyzedSha":"86d9c8fc543e7c56c9f624eb725f76c9baff9570","analyzedAt":"2026-09-12T00:46:39.097Z","contentChangedAt":"2026-09-12T00:46:39.097Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}