Skip to content

Commit

Permalink
Merge pull request tsawler#1 from tsawler/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
tsawler committed Mar 31, 2022
2 parents a43b06b + a86c03a commit b3b7754
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package toolbox

import (
"bytes"
"crypto/rand"
"encoding/json"
"errors"
Expand Down Expand Up @@ -87,3 +88,30 @@ func (t *Tools) RandomString(n int) string {
}
return string(s)
}

// PushJSONToRemote posts arbitrary json to some url, and returns error,
// if any, as well as the response status code
func (t *Tools) PushJSONToRemote(uri string, data any) (error, int) {
// create json we'll send
jsonData, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return err, 0
}

// build the request and set header
request, err := http.NewRequest("POST", uri, bytes.NewBuffer(jsonData))
if err != nil {
return err, 0
}
request.Header.Set("Content-Type", "application/json")

// call the uri
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return err, 0
}
defer response.Body.Close()

return nil, response.StatusCode
}

0 comments on commit b3b7754

Please sign in to comment.