Skip to content

Commit

Permalink
updates HttpPostJSON to support response decoding (#7)
Browse files Browse the repository at this point in the history
This is a breaking api change.

- Updated HttpPostJSON in json.go to handle JSON response decoding when content-type is JSON, with improved error handling and code clarity
- Added code in README to demonstrate HttpPutJSON and updated HttpPostJSON functionalities
  • Loading branch information
jritsema authored Jul 7, 2024
1 parent d1d21c1 commit d54589c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ app
*.swp
*.test
*.out
.DS_Store
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ func main() {

var data interface{}
err = gotoolbox.HttpGetJSON("https://api.example.com/data.json", &data)
err = gotoolbox.HttpPostJSON("https://api.example.com/data.json", data, http.StatusOK)

err = gotoolbox.HttpPutJSON("https://api.example.com/data.json", data)

var res Response
err = gotoolbox.HttpPostJSON("https://api.example.com/data.json", data, &res, http.StatusCreated)
}
```

Expand Down
29 changes: 21 additions & 8 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"os"
)

const contentType = "Content-Type"
const contentTypeJSON = "application/json"

//ReadJSONFile reads JSON from a file
func ReadJSONFile(fileName string) (map[string]string, error) {

Expand Down Expand Up @@ -51,7 +54,7 @@ func HttpGetJSON(url string, result interface{}) error {
}

// HttpPutJSON encodes a struct as JSON and
// HTTP PUTs it to the specified endpoint
// HTTP PUTs it to the specified url
// returns an error if http response code is not 200
func HttpPutJSON(url string, o interface{}) error {

Expand All @@ -61,7 +64,7 @@ func HttpPutJSON(url string, o interface{}) error {
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(contentType, contentTypeJSON)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand All @@ -75,25 +78,35 @@ func HttpPutJSON(url string, o interface{}) error {
}

// HttpPostJSON encodes a struct as JSON and
// HTTP POSTs it to the specified endpoint
// returns an error if http response code does not match specified
func HttpPostJSON(url string, o interface{}, httpStatusCodeToCheck int) error {
// HTTP POSTs it to the specified url.
// If the HTTP response's content-type is JSON,
// then the response is decoded into the given result.
// Returns an error if http response code does not match
// specified `httpStatusCodeToCheck`.
// Set `httpStatusCodeToCheck` == 0 to opt out of the check.
func HttpPostJSON(url string, request, response interface{}, httpStatusCodeToCheck int) error {

payload := new(bytes.Buffer)
json.NewEncoder(payload).Encode(o)
json.NewEncoder(payload).Encode(request)
req, err := http.NewRequest(http.MethodPost, url, payload)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(contentType, contentTypeJSON)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("unexpected http PUT error: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != httpStatusCodeToCheck {
if resp.StatusCode != 0 && resp.StatusCode != httpStatusCodeToCheck {
return fmt.Errorf("unexpected http POST status: %s", resp.Status)
}
if resp.Header.Get(contentType) == contentTypeJSON {
err = json.NewDecoder(resp.Body).Decode(response)
if err != nil {
return fmt.Errorf("cannot decode JSON: %w", err)
}
}
return nil
}

0 comments on commit d54589c

Please sign in to comment.