Last active
June 27, 2016 10:06
-
-
Save bboozzoo/5962c10277927eca9a80791795b28b5c to your computer and use it in GitHub Desktop.
Golang compressed HTTP cmpression unexpected EOF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
func main() { | |
cl := &http.Client{} | |
d := bytes.NewBufferString("foobar") | |
r, err := http.NewRequest(http.MethodPost, "http://localhost:9090/foo", d) | |
if err != nil { | |
log.Fatalf("request new failed: %v", err) | |
} | |
r.Header.Set("Content-Type", "application/json") | |
rsp, err := cl.Do(r) | |
if err != nil { | |
log.Fatalf("error: %v", err) | |
} | |
defer rsp.Body.Close() | |
data, err := ioutil.ReadAll(rsp.Body) | |
if err != nil { | |
log.Fatalf("read all error: %v", err) | |
} | |
log.Printf("data; %v", string(data)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
curl -X POST -d "zoo" -H 'Content-Type: application/json' http://localhost:9090/foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"github.com/ant0ine/go-json-rest/rest" | |
"log" | |
"net/http" | |
"os" | |
) | |
const ( | |
EnvProd = "prod" | |
EnvDev = "dev" | |
) | |
var ( | |
DefaultDevStack = []rest.Middleware{ | |
// logging | |
&rest.AccessLogApacheMiddleware{}, | |
&rest.TimerMiddleware{}, | |
&rest.RecorderMiddleware{}, | |
// catches the panic errors that occur with stack trace | |
&rest.RecoverMiddleware{ | |
EnableResponseStackTrace: true, | |
}, | |
// json pretty print | |
&rest.JsonIndentMiddleware{}, | |
// verifies the request Content-Type header | |
// The expected Content-Type is 'application/json' | |
// if the content is non-null | |
&rest.ContentTypeCheckerMiddleware{}, | |
} | |
DefaultProdStack = []rest.Middleware{ | |
// logging | |
&rest.AccessLogJsonMiddleware{ | |
// No prefix or other fields, entire output is JSON encoded and could break it. | |
Logger: log.New(os.Stderr, "", 0), | |
}, | |
&rest.TimerMiddleware{}, | |
&rest.RecorderMiddleware{}, | |
// catches the panic errors | |
&rest.RecoverMiddleware{}, | |
// response compression | |
&rest.GzipMiddleware{}, | |
// verifies the request Content-Type header | |
// The expected Content-Type is 'application/json' | |
// if the content is non-null | |
&rest.ContentTypeCheckerMiddleware{}, | |
} | |
middlewareMap = map[string][]rest.Middleware{ | |
EnvProd: DefaultProdStack, | |
EnvDev: DefaultDevStack, | |
} | |
) | |
func main() { | |
api := rest.NewApi() | |
mwtype := EnvProd | |
log.Printf("setting up %s middleware", mwtype) | |
mwstack, ok := middlewareMap[mwtype] | |
if ok != true { | |
log.Fatalf("incorrect middleware type: %s", mwtype) | |
} | |
api.Use(mwstack...) | |
routes := []*rest.Route{ | |
rest.Post("/foo", func(w rest.ResponseWriter, r *rest.Request) { | |
log.Printf("got request: %v", r) | |
w.WriteHeader(http.StatusOK) | |
w.WriteJson(struct { | |
Test string | |
}{ | |
"foobar", | |
}) | |
}), | |
} | |
app, err := rest.MakeRouter(routes...) | |
if err != nil { | |
log.Fatalf("app failed: %v", err) | |
} | |
api.SetApp(app) | |
log.Fatal(http.ListenAndServe(":9090", api.MakeHandler())) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment