Handle json requests to create API

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-05-10 23:00:08 +02:00
parent 37a933907f
commit b51293fe3d
No known key found for this signature in database
GPG Key ID: DC2729FDD34BE99E

18
api.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -23,7 +24,22 @@ func (a apiServer) Register(r *mux.Router) {
} }
func (a apiServer) handleCreate(res http.ResponseWriter, r *http.Request) { func (a apiServer) handleCreate(res http.ResponseWriter, r *http.Request) {
secret := r.FormValue("secret") var secret string
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
tmp := map[string]string{}
if err := json.NewDecoder(r.Body).Decode(&tmp); err != nil {
a.jsonResponse(res, http.StatusBadRequest, map[string]interface{}{
"success": false,
"error": err.Error(),
})
return
}
secret = tmp["secret"]
} else {
secret = r.FormValue("secret")
}
if secret == "" { if secret == "" {
a.jsonResponse(res, http.StatusBadRequest, map[string]interface{}{ a.jsonResponse(res, http.StatusBadRequest, map[string]interface{}{
"success": false, "success": false,