package main import ( "bytes" "flag" "io/ioutil" "log" "net" "net/http" "net/http/httputil" "strings" ) func curlUnixSocket(host string, data []byte) ([]byte, error) { protoAddrParts := strings.SplitN(host, "://", 2) contentReader := bytes.NewReader(data) // http://d/ //req, err := http.NewRequest("POST", "http://d/", contentReader) req, err := http.NewRequest("POST", "/d", contentReader) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") req.URL.Host = protoAddrParts[1] dial, err := net.Dial(protoAddrParts[0], protoAddrParts[1]) if err != nil { return nil, err } clientconn := httputil.NewClientConn(dial, nil) defer clientconn.Close() resp, err := clientconn.Do(req) //fakeDial := func (proto, addr string) (conn net.Conn, err error) { // return net.Dial("unix", protoAddrParts[1]) //} //tr := &http.Transport{ // Dial: fakeDial, //} //client := &http.Client{Transport: tr} //resp, err := client.Post("http://d", "application/json", contentReader) if err != nil { return nil, err } body, _ := ioutil.ReadAll(resp.Body) return body, nil } // be sure to install go1.4.3 to /usr/local/go1.4.3 // export PATH=/usr/local/go1.4.3/bin:$PATH // export GOROOT=/usr/local/go1.4.3 // go build -o client client.go // ./client # request unix socket unix:///run/test.sock // ./client -address tcp://127.0.0.1:80 # request tcp 80 func main() { address := flag.String("address", "unix:///run/test.sock", "the address to request") data := flag.String("data", "hello there", "the data to send to remote server") flag.Parse() body, err := curlUnixSocket(*address, []byte(*data)) if err != nil { log.Printf("%v", err) } else { log.Printf("body: %s", string(body)) } }