Skip to content

Conversation

yseto
Copy link
Contributor

@yseto yseto commented Jul 16, 2024

refer https://github.com/mackerelio/mackerel-client-go/releases/tag/v0.27.0

// PostJSON shortcut method for posting json
func (c *Client) PostJSON(path string, payload interface{}) (*http.Response, error) {
return c.requestJSON("POST", path, payload)
}
// PutJSON shortcut method for putting json
func (c *Client) PutJSON(path string, payload interface{}) (*http.Response, error) {
return c.requestJSON("PUT", path, payload)
}
func (c *Client) requestJSON(method string, path string, payload interface{}) (*http.Response, error) {
var body bytes.Buffer
err := json.NewEncoder(&body).Encode(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, c.urlFor(path).String(), &body)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
return c.Request(req)
}

@yseto yseto self-assigned this Jul 16, 2024
@yseto yseto merged commit ebbc66f into master Jul 16, 2024
10 checks passed
@yseto
Copy link
Contributor Author

yseto commented Jul 16, 2024

thanks your review.

@yseto yseto deleted the revert-useful-json-request branch July 16, 2024 03:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants