{"record":{"id":"7d3ba68f187b8999","repo":"valyala/fasthttp","slug":"error-when-marshaling-multipart-form-w","errorCode":null,"errorMessage":"error when marshaling multipart form: %w","messagePattern":"error when marshaling multipart form: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":1845,"sourceCode":"\t\t\tbuf = append(buf, uri.username...)\n\t\t\tbuf = append(buf, strColon...)\n\t\t\tbuf = append(buf, uri.password...)\n\t\t\tbuf = append(buf, strBasicSpace...)\n\t\t\tbase64.StdEncoding.Encode(buf[nb:tl], buf[:nl])\n\t\t\treq.Header.SetBytesKV(strAuthorization, buf[nl:tl])\n\t\t}\n\t}\n\n\tif req.bodyStream != nil {\n\t\treturn req.writeBodyStream(w)\n\t}\n\n\tbody := req.bodyBytes()\n\tvar err error\n\tif req.onlyMultipartForm() {\n\t\tbody, err = marshalMultipartForm(req.multipartForm, req.multipartFormBoundary)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"error when marshaling multipart form: %w\", err)\n\t\t}\n\t\treq.Header.SetMultipartFormBoundary(req.multipartFormBoundary)\n\t}\n\n\thasBody := false\n\tif len(body) == 0 {\n\t\tbody = req.postArgs.QueryString()\n\t}\n\tif len(body) != 0 || !req.Header.ignoreBody() {\n\t\thasBody = true\n\t\treq.Header.SetContentLength(len(body))\n\t}\n\tif err = req.Header.Write(w); err != nil {\n\t\treturn err\n\t}\n\tif hasBody {\n\t\t_, err = w.Write(body)\n\t} else if len(body) > 0 {","sourceCodeStart":1827,"sourceCodeEnd":1863,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L1827-L1863","documentation":"When writing a request that consists only of a multipart form, fasthttp marshals the form into the body; this error wraps any failure from that marshaling (part creation, file copy/close, writer close — i.e. errors 170-173 bubbling up). The request cannot be serialized and is not sent.","triggerScenarios":"client.Do / Request.Write with req.onlyMultipartForm() true (set via SetFormFile* APIs) when marshalMultipartForm fails for any of the underlying part-write reasons.","commonSituations":"Upload of a file that disappeared between SetFormFileCreate and Do; reusing a Request without Reset; failing custom readers in the multipart form.","solutions":["Unwrap the cause to find the specific part that failed (field name is embedded).","Ensure source files/readers are valid at client.Do time.","Reset the Request before each reuse.","Wrap client.Do in error handling that distinguishes request-build errors from network errors."],"exampleFix":"// before\nerr := client.Do(req, resp) // generic handling\n// after\nif err := client.Do(req, resp); err != nil {\n    var be *fasthttp.ErrSmallBuffer // example typed handling\n    log.Printf(\"request build/send failed: %v\", err)\n    req.Reset()\n}","handlingStrategy":"try-catch","validationCode":"for k, fhs := range req.MultipartForm.File {\n    for _, fh := range fhs {\n        if fh == nil { return fmt.Errorf(\"nil file header for %s\", k) }\n    }\n}","typeGuard":"func isRequestBuildError(err error) bool {\n    msg := err.Error()\n    return strings.Contains(msg, \"multipart form\") ||\n        strings.Contains(msg, \"form file\")\n}","tryCatchPattern":"if err := client.Do(req, resp); err != nil {\n    if strings.Contains(err.Error(), \"error when marshaling multipart form\") {\n        req.Reset()\n        return fmt.Errorf(\"rebuild upload request: %w\", err)\n    }\n    return err\n}","preventionTips":["Validate upload sources exist before Do","Reset reused requests","Distinguish build-time errors from transport errors in your client wrapper"],"tags":["multipart","request-marshaling"],"backgroundTag":"multipart-form-write-failed","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}