diff options
| author | Jeff Carr <[email protected]> | 2025-09-06 14:55:33 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-09-06 14:55:33 -0500 |
| commit | 6bb1c8be437e077adc28182aae8d547a14937105 (patch) | |
| tree | a2fb39bf1635a48af72cb3d950ed958ad0c8fb58 /post.go | |
| parent | f2515acf4ada1b9c12e33d0e92c5574a81db3ce1 (diff) | |
post() funcv0.0.1
Diffstat (limited to 'post.go')
| -rw-r--r-- | post.go | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -0,0 +1,55 @@ +// Copyright 1994-2025 WIT.COM Inc Licensed GPL 3.0 + +package httppb + +import ( + "bytes" + "io/ioutil" + "net/http" + "net/url" + "os" + "os/user" +) + +func HttpPost(baseURL string, route string, data []byte) ([]byte, error) { + // Fix using url.JoinPath (Best Practice) + tmpURL, _ := url.Parse(baseURL) // "http://forge.grid.wit.com:2520") + finalURL := tmpURL.JoinPath(route) // Correctly produces ...:2520/patches + + var err error + var req *http.Request + + req, err = http.NewRequest(http.MethodPost, finalURL.String(), bytes.NewBuffer(data)) + if req == nil { + return nil, err + } + + usr, _ := user.Current() + if os.Getenv("GIT_AUTHOR_NAME") == "" { + username = usr.Username + } else { + usernname = os.Getenv("GIT_AUTHOR_NAME") + } + req.Header.Set("author", username) + hostname, _ := os.Hostname() + req.Header.Set("hostname", hostname) + + return PostReq(req) +} + +// Posts a reqest and returns the bytes returned +func PostReq(req *http.Request) ([]byte, error) { + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return []byte("client.Do(req) error"), err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return body, err + } + + return body, nil +} |
