{"record":{"id":"417d5a594763a07d","repo":"kataras/iris","slug":"errnotfound","errorCode":"ErrNotFound","errorMessage":"unmarshal: empty body: %w","messagePattern":"unmarshal: empty body: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"context/context.go","lineNumber":2707,"sourceCode":"\treturn body, nil\n}\n\n// Validator is the validator for request body on Context methods such as\n// ReadJSON, ReadMsgPack, ReadXML, ReadYAML, ReadForm, ReadQuery, ReadBody and e.t.c.\ntype Validator interface {\n\tStruct(any) error\n\t// If community asks for more than a struct validation on JSON, XML, MsgPack, Form, Query and e.t.c\n\t// then we should add more methods here, alternative approach would be to have a\n\t// `Validator:Validate(any) error` and a map[reflect.Kind]Validator instead.\n}\n\n// UnmarshalBody reads the request's body and binds it to a value or pointer of any type\n// Examples of usage: context.ReadJSON, context.ReadXML.\n//\n// Example: https://github.com/kataras/iris/blob/main/_examples/request-body/read-custom-via-unmarshaler/main.go\nfunc (ctx *Context) UnmarshalBody(outPtr any, unmarshaler Unmarshaler) error {\n\tif ctx.request.Body == nil {\n\t\treturn fmt.Errorf(\"unmarshal: empty body: %w\", ErrNotFound)\n\t}\n\n\trawData, err := ctx.GetBody()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif decoderWithCtx, ok := outPtr.(BodyDecoderWithContext); ok {\n\t\treturn decoderWithCtx.DecodeContext(ctx.request.Context(), rawData)\n\t}\n\n\t// check if the v contains its own decode\n\t// in this case the v should be a pointer also,\n\t// but this is up to the user's custom Decode implementation*\n\t//\n\t// See 'BodyDecoder' for more.\n\tif decoder, isDecoder := outPtr.(BodyDecoder); isDecoder {\n\t\treturn decoder.Decode(rawData)","sourceCodeStart":2689,"sourceCodeEnd":2725,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/context/context.go#L2689-L2725","documentation":"Returned by Context.UnmarshalBody (the engine behind ctx.ReadJSON, ReadXML and custom Unmarshaler reads) when the request has no body at all (ctx.request.Body == nil). The library wraps the sentinel ErrNotFound, so callers can distinguish 'no body to unmarshal' from a decoding failure.","triggerScenarios":"POST/PUT requests with Content-Length 0 or no body calling ctx.ReadJSON/ReadXML/UnmarshalBody; requests where a prior handler already consumed and closed the body.","commonSituations":"Clients sending POST with empty payload (e.g. fetch without body on POST); middleware that consumed the body; tests forgetting to set a request body; curl -X POST without -d.","solutions":["Have clients always send a valid body for endpoints using ReadJSON/ReadXML (e.g. '{}').","Check errors.Is(err, context.ErrNotFound) and return 400 'empty request body'.","Use ctx.GetBody() once and decode manually if you need custom empty-body defaults.","In tests, set req.Body = io.NopCloser(strings.NewReader(...)) instead of nil."],"exampleFix":"// before\nvar p Product\nif err := ctx.ReadJSON(&p); err != nil { return err }\n// after\nvar p Product\nif err := ctx.ReadJSON(&p); err != nil {\n    if errors.Is(err, context.ErrNotFound) {\n        ctx.StatusCode(iris.StatusBadRequest)\n        return errors.New(\"request body is required\")\n    }\n    return err\n}","handlingStrategy":"validation","validationCode":"if ctx.Request().ContentLength == 0 {\n    return errors.New(\"request body is required\")\n}","typeGuard":"func hasRequestBody(ctx *context.Context) bool {\n    return ctx.Request().Body != nil && ctx.Request().ContentLength != 0\n}","tryCatchPattern":"var p Payload\nif err := ctx.ReadJSON(&p); err != nil {\n    if errors.Is(err, context.ErrNotFound) {\n        ctx.StatusCode(iris.StatusBadRequest)\n        return errors.New(\"empty request body\")\n    }\n    return err\n}","preventionTips":["Require clients to send a body (even '{}') on POST/PUT endpoints that decode payloads","Guard ContentLength == 0 before ReadJSON/ReadXML","In tests always set a non-nil req.Body"],"tags":["go","iris","request-body","json","validation"],"backgroundTag":"empty-request-body","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}