From bf031595e359548f0a0d7197140195f1486c168e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 6 Sep 2025 15:22:31 -0500 Subject: patchset http routines --- http.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'http.go') diff --git a/http.go b/http.go index 27d6e06..2cb8e09 100644 --- a/http.go +++ b/http.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net" "net/http" + "net/url" "os/user" "strings" @@ -14,29 +15,36 @@ import ( "go.wit.com/log" ) -func (f *Forge) HttpPost(url string, data []byte) ([]byte, error) { +func (f *Forge) HttpPost(base string, route string, data []byte) ([]byte, error) { + // Fix using url.JoinPath (Best Practice) + baseURL, _ := url.Parse(f.forgeURL) // "http://forge.grid.wit.com:2520") + finalURL := baseURL.JoinPath(route) // Correctly produces ...:2520/patches + var err error var req *http.Request - req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data)) - // log.Info("httpPost() with len", len(data), "url", url) + req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data)) + if req == nil { + return nil, err + } usr, _ := user.Current() req.Header.Set("author", usr.Username) req.Header.Set("hostname", f.hostname) + return rawHttpPost(req) +} + +func rawHttpPost(req *http.Request) ([]byte, error) { client := &http.Client{} resp, err := client.Do(req) if err != nil { - log.Error(err) return []byte("client.Do(req) error"), err } defer resp.Body.Close() - // log.Info("httpPost() with len", len(data)) body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Error(err) return body, err } -- cgit v1.2.3