summaryrefslogtreecommitdiff
path: root/http.go
diff options
context:
space:
mode:
Diffstat (limited to 'http.go')
-rw-r--r--http.go20
1 files changed, 14 insertions, 6 deletions
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
}