Created
September 14, 2017 11:23
-
-
Save shabinesh/2a80c6c7e28bce627e61d496bd073a9a to your computer and use it in GitHub Desktop.
adhoc RequestBin for dev
This file contains hidden or 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" | |
| "encoding/json" | |
| "fmt" | |
| "io/ioutil" | |
| "net/http" | |
| log "github.com/sirupsen/logrus" | |
| ) | |
| func reqHandler(w http.ResponseWriter, r *http.Request) { | |
| log.Infoln("-----------------START------------------------") | |
| log.Infof("%s %s", r.Method, r.URL.Path) | |
| b, err := ioutil.ReadAll(r.Body) | |
| if err != nil { | |
| fmt.Fprintf(w, `{"error":"%s"}`, err.Error()) | |
| return | |
| } | |
| contentType := http.DetectContentType(b) | |
| if contentType == "application/json" || r.Header.Get("Content-Type") == "application/json" { | |
| var bx []byte | |
| buf := bytes.NewBuffer(bx) | |
| json.Indent(buf, b, "", " ") | |
| log.Infof("\n%s\n", buf.String()) | |
| } else { | |
| log.Infof("%s\n", b) | |
| } | |
| log.Infof("------------------END-------------------------") | |
| } | |
| func main() { | |
| m := http.NewServeMux() | |
| m.HandleFunc("/", reqHandler) | |
| http.ListenAndServe(":8080", m) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment