{"record":{"id":"506fe77f7ce567ae","repo":"gorilla/websocket","slug":"websocket-protocol-q-was-given-but-is-not-suppor","errorCode":null,"errorMessage":"websocket: protocol %q was given but is not supported;sharing tls.Config with net/http Transport can cause this error: %w","messagePattern":"websocket: protocol %q was given but is not supported;sharing tls\\.Config with net/http Transport can cause this error: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"client.go","lineNumber":343,"sourceCode":"\n\tconn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize, d.WriteBufferPool, nil, nil)\n\n\tif err := req.Write(netConn); err != nil {\n\t\treturn nil, nil, err\n\t}\n\n\tif trace != nil && trace.GotFirstResponseByte != nil {\n\t\tif peek, err := conn.br.Peek(1); err == nil && len(peek) == 1 {\n\t\t\ttrace.GotFirstResponseByte()\n\t\t}\n\t}\n\n\tresp, err := http.ReadResponse(conn.br, req)\n\tif err != nil {\n\t\tif d.TLSClientConfig != nil {\n\t\t\tfor _, proto := range d.TLSClientConfig.NextProtos {\n\t\t\t\tif proto != \"http/1.1\" {\n\t\t\t\t\treturn nil, nil, fmt.Errorf(\n\t\t\t\t\t\t\"websocket: protocol %q was given but is not supported;\"+\n\t\t\t\t\t\t\t\"sharing tls.Config with net/http Transport can cause this error: %w\",\n\t\t\t\t\t\tproto, err,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn nil, nil, err\n\t}\n\n\tif d.Jar != nil {\n\t\tif rc := resp.Cookies(); len(rc) > 0 {\n\t\t\td.Jar.SetCookies(u, rc)\n\t\t}\n\t}\n\n\tif resp.StatusCode != 101 ||\n\t\t!tokenListContainsValue(resp.Header, \"Upgrade\", \"websocket\") ||","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/gorilla/websocket/blob/e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f/client.go#L325-L361","documentation":"DialContext wraps the error from http.ReadResponse with this message when a TLS NextProtos entry other than \"http/1.1\" is configured. The library requires that the TLS config used for the WebSocket dial only negotiates HTTP/1.1; if an ALPN protocol like h2 is listed first, the TLS handshake may negotiate it and the expected HTTP/1.1 upgrade response never arrives, so the response read fails.","triggerScenarios":"Sharing a *tls.Config whose NextProtos contains e.g. \"h2\" with Dialer.TLSClientConfig (often the same config used by an http.Transport), then the ALPN negotiation picks a non-HTTP/1.1 protocol and http.ReadResponse fails, producing this wrapped error.","commonSituations":"Reusing an application-wide tls.Config from an HTTP/2-enabled net/http Transport, default NextProtos lists that include h2, or copying TLS config from an existing HTTP client into the WebSocket dialer.","solutions":["Clone the tls.Config for the dialer and set NextProtos to []string{\"http/1.1\"} (or remove non-http/1.1 entries)","Do not share a single tls.Config between net/http Transport and websocket.Dialer","If you control the server, ensure it supports the ALPN fallback the client offers","Inspect the inner error via errors.Unwrap to confirm the underlying response-read failure"],"exampleFix":"// before\ndialer := websocket.Dialer{TLSClientConfig: sharedTLSConfig}\n// after\ncfg := sharedTLSConfig.Clone()\ncfg.NextProtos = []string{\"http/1.1\"}\ndialer := websocket.Dialer{TLSClientConfig: cfg}","handlingStrategy":"fallback","validationCode":"func wsTLSConfig(base *tls.Config) *tls.Config {\n    cfg := base.Clone()\n    cfg.NextProtos = []string{\"http/1.1\"}\n    return cfg\n}","typeGuard":null,"tryCatchPattern":"dialer := websocket.Dialer{TLSClientConfig: wsTLSConfig(sharedTLSConfig)}\n_, resp, err := dialer.DialContext(ctx, url, nil)\nif err != nil && strings.Contains(err.Error(), \"was given but is not supported\") {\n    return fmt.Errorf(\"tls config shares ALPN protos with http transport: %w\", err)\n}","preventionTips":["Never share a tls.Config between http.Transport and websocket.Dialer — always Clone","Force NextProtos to [\"http/1.1\"] for websocket dials","Keep HTTP/2 ALPN config confined to your HTTP client setup"],"tags":["websocket","tls","alpn","configuration"],"backgroundTag":"tls-alpn-protocol-mismatch","analyzedSha":"e064f32e3674d9d79a8fd417b5bc06fa5c6cad8f","analyzedAt":"2026-08-31T12:40:58.222Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}