Skip to content

Instantly share code, notes, and snippets.

@nwjlyons
Created September 28, 2017 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwjlyons/121063467e11d0c30e7561357b6ea24f to your computer and use it in GitHub Desktop.
Save nwjlyons/121063467e11d0c30e7561357b6ea24f to your computer and use it in GitHub Desktop.
Print HTTP request to stdout
package main
import (
"net"
"os"
"log"
"fmt"
"io"
)
func main() {
port := 8080
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatalf("could not listen to tcp connections on port %d: %v", port, err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("could not accept connection: %v", err)
}
go func(){
io.Copy(os.Stdout, conn)
}()
}
}
@nwjlyons
Copy link
Author

For debugging purposes it is useful to see the request that gets sent.

eg. I was trying to send this request and it was failing because # isn't allowed in JSON in PyCharm. When I fired the request to the echo.go server above I could see why.

POST http://localhost:8080/
Content-Type: application/json

{
    "email": "foo.bar@example.com",
    "password": "pass#word"
}

This is what was sent

POST / HTTP/1.1
Content-Type: application/json
Content-Length: 60
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_152-release)
Accept-Encoding: gzip,deflate

{
    "email": "foo.bar@example.com",
    "password": "pass}

@nwjlyons
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment