{"record":{"id":"979b6a5a2c7cd8ab","repo":"valyala/fasthttp","slug":"fasthttp-cannot-find-http-request-method","errorCode":null,"errorMessage":"fasthttp: cannot find http request method","messagePattern":"fasthttp: cannot find http request method","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"header.go","lineNumber":474,"sourceCode":"// 1. necessary for message framing (e.g., Transfer-Encoding and Content-Length),\n// 2. routing (e.g., Host),\n// 3. request modifiers (e.g., controls and conditionals in Section 5 of [RFC7231]),\n// 4. authentication (e.g., see [RFC7235] and [RFC6265]),\n// 5. response control data (e.g., see Section 7.1 of [RFC7231]),\n// 6. determining how to process the payload (e.g., Content-Encoding, Content-Type, Content-Range, and Trailer)\n//\n// Return ErrBadTrailer if contain any forbidden trailers.\nfunc (h *header) AddTrailer(trailer string) error {\n\treturn h.AddTrailerBytes(s2b(trailer))\n}\n\nvar (\n\tErrBadTrailer                    = errors.New(\"fasthttp: contain forbidden trailer\")\n\tErrReadingResponseHeaders        = errors.New(\"fasthttp: error when reading response headers\")\n\tErrReadingResponseTrailer        = errors.New(\"fasthttp: error when reading response trailer\")\n\tErrResponseFirstLineMissingSpace = errors.New(\"fasthttp: cannot find whitespace in the first line of response\")\n\tErrUnexpectedStatusCodeChar      = errors.New(\"fasthttp: unexpected char at the end of status code\")\n\tErrMissingRequestMethod          = errors.New(\"fasthttp: cannot find http request method\")\n\tErrUnsupportedRequestMethod      = errors.New(\"fasthttp: unsupported http request method\")\n\tErrExtraWhitespaceInRequestLine  = errors.New(\"fasthttp: extra whitespace in request line\")\n\tErrEmptyRequestURI               = errors.New(\"fasthttp: requesturi cannot be empty\")\n\tErrDuplicateContentLength        = errors.New(\"fasthttp: duplicate content-length header\")\n\tErrUnsupportedTransferEncoding   = errors.New(\"fasthttp: unsupported transfer-encoding\")\n\tErrNonNumericChars               = errors.New(\"fasthttp: non-numeric chars found\")\n\tErrNeedMore                      = errors.New(\"fasthttp: need more data: cannot find trailing lf\")\n\tErrSmallReadBuffer               = errors.New(\"fasthttp: small read buffer. increase readbuffersize\")\n)\n\n// AddTrailerBytes add Trailer header value for chunked response\n// to indicate which headers will be sent after the body.\n//\n// Use Set to set the trailer header later.\n//\n// Trailers are only supported with chunked transfer.\n// Trailers allow the sender to include additional headers at the end of chunked messages.\n//","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/valyala/fasthttp/blob/c96f600972c6f4a7a30d664257b340ebe9d60124/header.go#L456-L492","documentation":"fasthttp requires every HTTP/1.x request to carry a method token before the request-URI. When parsing or validating a Request whose method is empty (RequestHeader.Method is empty string and no default was applied), ErrMissingRequestMethod is returned. It signals an incomplete request line that cannot be serialized/parsed.","triggerScenarios":"Manually building a RequestHeader/Request without setting Method (h.SetMethod or req.Header.Method) and then writing/reading it; constructing a raw request buffer via header.AppendBytes or Write where the method field was never populated; parsing a malformed incoming request with no method token.","commonSituations":"Low-level RequestHeader manipulation (e.g. proxy code that rebuilds headers), forgetting to call req.Header.SetMethod on a reused zero-value Request, or a hostile client sending a request line missing the method to your fasthttp server.","solutions":["Call req.Header.SetMethod(fasthttp.MethodGet) (or the appropriate method) before sending the request.","For reused/reset Request objects, re-set Method after Reset since Reset clears it.","If building headers by hand, append the method token first: 'GET /path HTTP/1.1\\r\\n'.","On the server side, return 400 to clients that omit the method instead of treating it as an application bug."],"exampleFix":"// before\nvar req fasthttp.Request\nreq.SetRequestURI(\"/api/v1\") // no method set\n// after\nvar req fasthttp.Request\nreq.Header.SetMethod(fasthttp.MethodPost)\nreq.SetRequestURI(\"/api/v1\")","handlingStrategy":"validation","validationCode":"if req.Header.Method == \"\" {\n    req.Header.SetMethod(fasthttp.MethodGet) // or reject\n}\nif !knownMethods[req.Header.Method] {\n    return errors.New(\"method missing or invalid\")\n}","typeGuard":"func hasValidMethod(h *fasthttp.RequestHeader) bool {\n    m := string(h.Method())\n    switch m {\n    case \"GET\", \"POST\", \"PUT\", \"DELETE\", \"HEAD\", \"OPTIONS\", \"PATCH\", \"CONNECT\", \"TRACE\":\n        return true\n    }\n    return false\n}","tryCatchPattern":"if err := client.Do(req, resp); err == fasthttp.ErrMissingRequestMethod {\n    return fmt.Errorf(\"request built without method: %w\", err)\n}","preventionTips":["Always set the method explicitly with fasthttp.Method* constants.","After Reset() on reused Requests, re-set Method along with URI.","Add a small builder wrapper that defaults Method to GET and never exposes a zero-value Request.","Unit-test request construction paths to catch missing fields."],"tags":["http","fasthttp","request-validation"],"backgroundTag":"invalid-http-request","analyzedSha":"c96f600972c6f4a7a30d664257b340ebe9d60124","analyzedAt":"2026-08-31T22:48:28.265Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}