1
0
Bifurcation 0

better way to deal with empty bodies

Cette révision appartient à :
Stephen McQuay 2013-12-28 23:01:10 -08:00
Parent ae10b4087a
révision efdae319fe
1 fichiers modifiés avec 4 ajouts et 15 suppressions

19
api.go
Voir le fichier

@ -2,8 +2,6 @@ package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
@ -32,6 +30,7 @@ func apiInfo(w http.ResponseWriter, req *http.Request) {
Name: "itslog",
Routes: []string{
"/api/v0/info/",
"/api/v0/put/",
},
}
if err := json.NewEncoder(w).Encode(version); err != nil {
@ -46,24 +45,14 @@ func put(w http.ResponseWriter, req *http.Request) {
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("unable to read request body:", err)
}
req.Body.Close()
message := LogMessage{}
if *verbose {
log.Printf("incoming: '%s'", body)
}
parse_err := json.Unmarshal(body, &message)
if parse_err != nil {
if err := json.NewDecoder(req.Body).Decode(&message); err != nil {
b, _ := json.Marshal(JSONError{err.Error()})
http.Error(w, string(b), http.StatusMethodNotAllowed)
http.Error(w, string(b), http.StatusBadRequest)
return
}
_, err = message.save()
_, err := message.save()
if err != nil {
b, _ := json.Marshal(JSONError{err.Error()})
http.Error(w, string(b), http.StatusMethodNotAllowed)