{"record":{"id":"4e7d459c3747b0d2","repo":"valyala/fasthttp","slug":"cannot-open-form-file-q-q-w","errorCode":null,"errorMessage":"cannot open form file %q (%q): %w","messagePattern":"cannot open form file %q \\(%q\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"http.go","lineNumber":1251,"sourceCode":"\t// marshal values\n\tfor k, vv := range f.Value {\n\t\tfor _, v := range vv {\n\t\t\tif err := mw.WriteField(k, v); err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot write form field %q value %q: %w\", k, v, err)\n\t\t\t}\n\t\t}\n\t}\n\n\t// marshal files\n\tfor k, fvv := range f.File {\n\t\tfor _, fv := range fvv {\n\t\t\tvw, err := mw.CreatePart(fv.Header)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot create form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t\tfh, err := fv.Open()\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot open form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t\tif _, err = copyZeroAlloc(vw, fh); err != nil {\n\t\t\t\t_ = fh.Close()\n\t\t\t\treturn fmt.Errorf(\"error when copying form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t\tif err = fh.Close(); err != nil {\n\t\t\t\treturn fmt.Errorf(\"cannot close form file %q (%q): %w\", k, fv.Filename, err)\n\t\t\t}\n\t\t}\n\t}\n\n\tif err := mw.Close(); err != nil {\n\t\treturn fmt.Errorf(\"error when closing multipart form writer: %w\", err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":1233,"sourceCodeEnd":1269,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/http.go#L1233-L1269","documentation":"fasthttp wraps the error returned by multipart.Writer.CreatePart (and, in the sibling message, part file Open) when writing a multipart/form-data request body. The quoted form field name, the original filename, and the underlying cause are embedded so the caller can identify which part failed. It signals the multipart writer could not emit the part header for this form file.","triggerScenarios":"Calling Request.SetFormFile / SetFormFileContent / SetFormFileCreate (or MultipartForm) and then writing the request, when the multipart writer's CreatePart fails while creating the part for form field k with filename fv.Filename — e.g. malformed part headers or writer already closed.","commonSituations":"Reusing a Request whose previous multipart write already closed the writer; corrupt MultipartForm state after reuse without Reset; very large form value headers; bugs in custom FormFile implementations returning bad headers.","solutions":["Call req.Reset() (or use a fresh Request) before reusing it for another multipart request.","Inspect the wrapped cause (%w) to fix the underlying CreatePart error.","Verify the FormFile value passed to SetFormFile has a valid Filename and Open()-able content.","Update fasthttp — multipart writer edge cases have been fixed over time."],"exampleFix":"// before\nreq := acquireRequest()\nreq.SetFormFile(req.MultipartForm, \"file\", fh) // reused request\n// after\nreq.Reset()\nreq.SetFormFile(\"file\", fh)\nif err := client.Do(req, resp); err != nil {\n    log.Printf(\"multipart create part failed: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"if req.MultipartForm != nil { for k, fv := range req.MultipartForm.File { if fv == nil { return fmt.Errorf(\"nil form file %s\", k) } } }","typeGuard":"func validFormFile(f *multipart.FileHeader) bool { return f != nil && f.Filename != \"\" }","tryCatchPattern":"if err := client.Do(req, resp); err != nil {\n    if strings.Contains(err.Error(), \"cannot create form file\") {\n        req.Reset(); return retryWithFreshRequest()\n    }\n    return err\n}","preventionTips":["Reset or recreate Requests between uses","Keep fasthttp updated","Pass well-formed FormFile values with valid filenames"],"tags":["multipart","form-data","request-writing"],"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"}