diff options
| author | Jeff Carr <[email protected]> | 2025-09-06 15:22:31 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-09-06 15:22:31 -0500 |
| commit | bf031595e359548f0a0d7197140195f1486c168e (patch) | |
| tree | 13805532605edb8e978389735c5b28f982186324 /http.go | |
| parent | c0b9518c0db72aa40e4e37e3d90341b32b364bd5 (diff) | |
patchset http routines
Diffstat (limited to 'http.go')
| -rw-r--r-- | http.go | 20 |
1 files changed, 14 insertions, 6 deletions
@@ -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 } |
