diff options
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 } |
