1
0
Fork 0

🚑 Fixed content-type of requests

This commit is contained in:
Maxim Lebedev 2017-10-17 17:35:48 +05:00
parent 62cec7b9b1
commit 706baf4a24
No known key found for this signature in database
GPG Key ID: F8978F46FF0FFA4F
1 changed files with 16 additions and 4 deletions

View File

@ -14,18 +14,30 @@ const (
)
func (bot *Bot) request(dst []byte, method string, args *http.Args) (*Response, error) {
_, resp, err := http.Post(dst, fmt.Sprintf(APIEndpoint, bot.AccessToken, method), args)
if err != nil {
requestURI := fmt.Sprintf(APIEndpoint, bot.AccessToken, method)
if &args != nil {
requestURI += fmt.Sprint("?", args.String())
}
var req http.Request
var resp http.Response
req.Header.SetMethod("POST")
req.Header.SetContentType("application/json")
req.SetRequestURI(requestURI)
req.SetBody(dst)
if err := http.Do(&req, &resp); err != nil {
return nil, err
}
var data Response
if err := json.Unmarshal(resp, &data); err != nil {
if err := json.Unmarshal(resp.Body(), &data); err != nil {
return nil, err
}
if !data.Ok {
return &data, errors.New(data.Description)
return nil, errors.New(data.Description)
}
return &data, nil