geektutu/7days-golang · error
unexpected HTTP response:
Error message
unexpected HTTP response:
What it means
NewHTTPClient dials an RPC server over HTTP CONNECT. After sending CONNECT it reads the server's HTTP response; if the response is not the expected 'Connected to Gee RPC' status, the library wraps the raw status line into this error. It means the handshake to switch from HTTP to the RPC protocol failed.
Source
Thrown at gee-rpc/day7-registry/client.go:295
}
// Dial connects to an RPC server at the specified network address
func Dial(network, address string, opts ...*Option) (*Client, error) {
return dialTimeout(NewClient, network, address, opts...)
}
// NewHTTPClient new a Client instance via HTTP as transport protocol
func NewHTTPClient(conn net.Conn, opt *Option) (*Client, error) {
_, _ = io.WriteString(conn, fmt.Sprintf("CONNECT %s HTTP/1.0\n\n", defaultRPCPath))
// Require successful HTTP response
// before switching to RPC protocol.
resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
if err == nil && resp.Status == connected {
return NewClient(conn, opt)
}
if err == nil {
err = errors.New("unexpected HTTP response: " + resp.Status)
}
return nil, err
}
// DialHTTP connects to an HTTP RPC server at the specified network address
// listening on the default HTTP RPC path.
func DialHTTP(network, address string, opts ...*Option) (*Client, error) {
return dialTimeout(NewHTTPClient, network, address, opts...)
}
// XDial calls different functions to connect to a RPC server
// according the first parameter rpcAddr.
// rpcAddr is a general format (protocol@addr) to represent a rpc server
// eg, http@10.0.0.1:7001, tcp@10.0.0.1:9999, unix@/tmp/geerpc.sock
func XDial(rpcAddr string, opts ...*Option) (*Client, error) {
parts := strings.Split(rpcAddr, "@")
if len(parts) != 2 {
return nil, fmt.Errorf("rpc client err: wrong format '%s', expect protocol@addr", rpcAddr)View on GitHub (pinned to cf36443821)
Solutions
- Verify the server address points to a GeeRPC server started via http.ListenAndServe (StartHTTP mode), not a normal TCP RPC port or unrelated web server
- Check the full error string: resp.Status tells you what the server actually returned (e.g. 404) and fix routing/port accordingly
- Remove or reconfigure proxies/load balancers between client and server that do not forward CONNECT
- Confirm client and server use the same gee-rpc version so the magic 'connected' status string matches
Example fix
// before
client, err := geerpc.XDialHTTP("tcp", "localhost:9999", geerpc.GeeOption{...}) // 9999 is a plain web server
// after
client, err := geerpc.XDialHTTP("tcp", "localhost:7000", geerpc.GeeOption{...}) // 7000 runs gee-rpc in HTTP mode Defensive patterns
Strategy: try-catch
Validate before calling
// Go has no pre-call hook into the handshake; verify the endpoint before dialing
func isGeeRPC Compatible(addr string) bool {
conn, err := net.DialTimeout("tcp", addr, 2*time.Second)
if err != nil { return false }
conn.Close()
return true
}
// additionally confirm the target is your gee-rpc HTTP server port, not a web server Try / catch
client, err := geerpc.NewHTTPClient(conn)
if err != nil {
if strings.Contains(err.Error(), "unexpected HTTP response") {
log.Fatalf("endpoint %s is not a gee-rpc HTTP server: %v", addr, err)
}
return err
} Prevention
- Run gee-rpc servers in HTTP mode on dedicated, documented ports
- Keep client and server gee-rpc versions in sync
- Avoid proxies that mangle CONNECT between client and server
- Alert on this error at startup rather than failing silently
When it happens
Trigger: Calling NewHTTPClient (via XDialHTTP or DialHTTP) against an address that is not a GeeRPC HTTP server, a server returning a non-101/200 CONNECT response, a plain HTTP endpoint that replies with e.g. '404 Not Found' or '400 Bad Request' to CONNECT, or a proxy/gateway intercepting the CONNECT request.
Common situations: Pointing the client at the wrong port or at a regular REST/web server; firewalls or reverse proxies rejecting CONNECT; connecting to a GeeRPC server started without the HTTP mode; version mismatch where the server's connected status string differs.
Related errors
- reading body
- unexpected HTTP response:
- unexpected HTTP response:
- server returned: %v
- reading response body: %v
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/a42312ec1ad84400.
Report an issue: GitHub.