examples: pretty print JSON in webhook

This commit is contained in:
Fabian Reinartz 2016-09-05 11:17:28 +02:00
parent 4ac976c7e6
commit 44bde96447
1 changed files with 8 additions and 3 deletions

View File

@ -1,19 +1,24 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
http.ListenAndServe(":5001", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("received request:", r.Header)
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
defer r.Body.Close()
log.Println("request body:", string(b))
var buf bytes.Buffer
if err := json.Indent(&buf, b, " >", " "); err != nil {
panic(err)
}
fmt.Println(buf.String())
}))
}