From f8e1dddc49ee220a49222719a4ed34753ecb8295 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Sun, 26 Nov 2017 20:08:26 +0000 Subject: [PATCH] Add helper to make JSON HTTP requests --- clientutil/json.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 clientutil/json.go diff --git a/clientutil/json.go b/clientutil/json.go new file mode 100644 index 0000000..862d086 --- /dev/null +++ b/clientutil/json.go @@ -0,0 +1,40 @@ +package clientutil + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "net/http" +) + +func DoJSONHTTPRequest(client *http.Client, uri string, req, resp interface{}) error { + data, err := json.Marshal(req) + if err != nil { + return err + } + + httpReq, err := http.NewRequest("POST", uri, bytes.NewReader(data)) + if err != nil { + return err + } + httpReq.Header.Set("Content-Type", "application/json") + + httpResp, err := RetryHTTPDo(client, httpReq, NewExponentialBackOff()) + if err != nil { + return err + } + defer httpResp.Body.Close() + + if httpResp.StatusCode != 200 { + return fmt.Errorf("HTTP status %d", httpResp.StatusCode) + } + if httpResp.Header.Get("Content-Type") != "application/json" { + return errors.New("not a JSON response") + } + + if resp == nil { + return nil + } + return json.NewDecoder(httpResp.Body).Decode(resp) +} -- GitLab