better way to deal with empty bodies
Dieser Commit ist enthalten in:
Ursprung
ae10b4087a
Commit
efdae319fe
19
api.go
19
api.go
|
@ -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)
|
||||
|
|
Laden…
In neuem Issue referenzieren