Skip to content

Instantly share code, notes, and snippets.

@shabinesh
Created September 14, 2017 11:23
Show Gist options
  • Select an option

  • Save shabinesh/2a80c6c7e28bce627e61d496bd073a9a to your computer and use it in GitHub Desktop.

Select an option

Save shabinesh/2a80c6c7e28bce627e61d496bd073a9a to your computer and use it in GitHub Desktop.
adhoc RequestBin for dev
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