freezingfire
freezingfire

Reputation: 23

Failure to do a simple HTTP POST using the net library in GO. What am I doing wrong?

I'm trying to do a simple RAW data UPLOAD using the net library (not the net/http). I have a simple php script that just spits out whatever was sent to it. The problem is that the php script doesn't receive anything. What am I doing wrong?

conn, err := net.Dial("tcp", "127.0.0.1:80" )
fmt.Fprintf(conn, "POST /handleupload.php HTTP/1.0\r\n\r\n")
n, err := conn.Write([]byte("ABCDEFGHIJ"))
status, err := bufio.NewReader(conn).ReadString('z')
fmt.Println( status )

Upvotes: 0

Views: 51

Answers (1)

Thundercat
Thundercat

Reputation: 120941

Because the Content-Length header is absent, the server is waiting to read to EOF before completing the request. Try this:

conn, err := net.Dial("tcp", "127.0.0.1:80" )
fmt.Fprintf(conn, "POST /handleupload.php HTTP/1.0\r\n\r\n")
n, err := conn.Write([]byte("ABCDEFGHIJ"))
if c, ok := conn.(*net.TCPConn); ok {
    c.CloseWrite()
}
status, err := bufio.NewReader(conn).ReadString('z')
fmt.Println( status )

Another option is to write a Content-Length request header.

Upvotes: 1

Related Questions