Skip to content

Commit ebbc66f

Browse files
authored
Merge pull request #159 from mackerelio/revert-useful-json-request
revert PostJSON, PutJSON
2 parents 5094607 + 6faebaa commit ebbc66f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mackerel.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,28 @@ func requestInternal[T any](client *Client, method, path string, params url.Valu
194194
}
195195
return &data, resp.Header, nil
196196
}
197+
198+
func (c *Client) compatRequestJSON(method string, path string, payload interface{}) (*http.Response, error) {
199+
var body bytes.Buffer
200+
err := json.NewEncoder(&body).Encode(payload)
201+
if err != nil {
202+
return nil, err
203+
}
204+
205+
req, err := http.NewRequest(method, c.urlFor(path, url.Values{}).String(), &body)
206+
if err != nil {
207+
return nil, err
208+
}
209+
req.Header.Add("Content-Type", "application/json")
210+
return c.Request(req)
211+
}
212+
213+
// Deprecated: use other prefered method.
214+
func (c *Client) PostJSON(path string, payload interface{}) (*http.Response, error) {
215+
return c.compatRequestJSON(http.MethodPost, path, payload)
216+
}
217+
218+
// Deprecated: use other prefered method.
219+
func (c *Client) PutJSON(path string, payload interface{}) (*http.Response, error) {
220+
return c.compatRequestJSON(http.MethodPut, path, payload)
221+
}

0 commit comments

Comments
 (0)