I've read this so question and the example from this so question
Here's my demo:
package main
import (
"net/http"
)
func main() {
http.HandleFunc("/", handler1)
_ = http.ListenAndServe(":8008", nil)
}
func handler1(writer http.ResponseWriter, request *http.Request) {
hijacker, _ := writer.(http.Hijacker)
conn, buf, _ := hijacker.Hijack()
defer conn.Close()
_, _ = buf.WriteString("hello world")
_ = buf.Flush()
}
I ran the code with go run serv1.go
and sent request with curl
, but got HTTP/1.1 502 Bad Gateway
. Just wondering if the API has been changed or anything I'd missed?
❯ curl -i "http://localhost:8008/"
HTTP/1.1 502 Bad Gateway
Connection: keep-alive
Keep-Alive: timeout=4
Proxy-Connection: keep-alive
Content-Length: 0