From b51293fe3da55e574d24bfaa0090c584d697acfa Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 10 May 2019 23:00:08 +0200 Subject: [PATCH] Handle json requests to create API Signed-off-by: Knut Ahlers --- api.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/api.go b/api.go index 887e981..86e061d 100644 --- a/api.go +++ b/api.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "net/http" + "strings" "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) { - 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 == "" { a.jsonResponse(res, http.StatusBadRequest, map[string]interface{}{ "success": false,