{"id":"9e5db863c6beef67","repo":"gofiber/fiber","slug":"failed-to-unmarshal-xml-w","errorCode":null,"errorMessage":"failed to unmarshal xml: %w","messagePattern":"failed to unmarshal xml: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binder/xml.go","lineNumber":22,"sourceCode":"\t\"fmt\"\n\n\t\"github.com/gofiber/utils/v2\"\n)\n\n// XMLBinding is the XML binder for XML request body.\ntype XMLBinding struct {\n\tXMLDecoder utils.XMLUnmarshal\n}\n\n// Name returns the binding name.\nfunc (*XMLBinding) Name() string {\n\treturn \"xml\"\n}\n\n// Bind parses the request body as XML and returns the result.\nfunc (b *XMLBinding) Bind(body []byte, out any) error {\n\tif err := b.XMLDecoder(body, out); err != nil {\n\t\treturn fmt.Errorf(\"failed to unmarshal xml: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// Reset resets the XMLBinding binder.\nfunc (b *XMLBinding) Reset() {\n\tb.XMLDecoder = nil\n}\n","sourceCodeStart":4,"sourceCodeEnd":32,"githubUrl":"https://github.com/gofiber/fiber/blob/9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c/binder/xml.go#L4-L32","documentation":"Returned by XMLBinding.Bind (binder/xml.go:22) when the configured XMLDecoder (by default encoding/xml.Unmarshal) fails to parse the request body into the target struct. Fiber wraps the underlying xml.Unmarshal error so callers see both the parse failure and the wrap context. It surfaces during BodyParser/Context binding when the Content-Type is application/xml or the XML binder is explicitly selected.","triggerScenarios":"Calling app.BodyParser(ctx, &structPtr) on a request whose Content-Type is application/xml but whose body is malformed, truncated, uses attributes the struct cannot receive, or declares a different encoding (e.g. UTF-16 BOM). Also triggered by a charset declaration xml.Unmarshal cannot handle, or by a body that does not match the struct's xml tags.","commonSituations":"Client sends Content-Type: application/xml with a JSON body or plain text; the XML lacks a single root element; field names in the XML do not match the struct's xml:\"name\" tags; the body is gzip/compressed but not decoded first; version change where a field type changed and the old payload no longer unmarshals.","solutions":["Validate the request body is well-formed XML (single root element, properly closed tags) before or right after parsing.","Confirm struct fields carry correct xml:\"...\" tags matching the incoming element/attribute names.","Check Content-Type and decompress/charset-convert the body before calling BodyParser if the client sends gzip or a non-UTF-8 encoding.","Return a 400 with the wrapped error so clients can correct their payload."],"exampleFix":"// before\nif err := ctx.BodyParser(&payload); err != nil {\n    return err // opaque 500\n}\n\n// after — select XML explicitly and report a clear 400\nif err := ctx.BodyParser(&payload); err != nil {\n    return fiber.NewError(fiber.StatusBadRequest, \"invalid XML body: \"+err.Error())\n}","handlingStrategy":"try-catch","validationCode":"// Pre-validate the body is well-formed XML before binding.\nfunc isWellFormedXML(body []byte) bool {\n    dec := xml.NewDecoder(bytes.NewReader(body))\n    for {\n        if _, err := dec.Token(); err == io.EOF {\n            return true\n        } else if err != nil {\n            return false\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := ctx.BodyParser(&payload); err != nil {\n    if strings.Contains(err.Error(), \"failed to unmarshal xml\") {\n        return fiber.NewError(fiber.StatusBadRequest, \"malformed XML body\")\n    }\n    return err\n}","preventionTips":["Document and enforce the exact XML schema clients must send.","Verify Content-Type matches application/xml before attempting XML binding.","Decompress and charset-convert bodies before parsing when clients may send gzip or non-UTF-8 encodings.","Always return 400 (not 500) for unmarshal failures so clients can fix their payload."],"tags":["binder","xml","unmarshal","body-parser"],"analyzedSha":"9a4c7e57fe0b080a04235d28a4b0d2b4b353d58c","analyzedAt":"2026-08-04T21:44:03.395Z","schemaVersion":2}