Skip to content

Commit 19b9fa7

Browse files
committed
fix: 获取到http response后未对status code做有效判断
1 parent fa1dc40 commit 19b9fa7

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

bot.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func (bot *Bot) send(msg interface{}) (err error) {
7676
return resData.ToError()
7777
}
7878

79+
// isSuccess 返回 http 请求是否成功
80+
func isSuccess(statusCode int) bool {
81+
return statusCode >= http.StatusOK && statusCode < http.StatusMultipleChoices
82+
}
83+
7984
func (bot *Bot) doPost(url string, reqHeader map[string]string, reqBody io.Reader, resData interface{}) error {
8085
req, err := http.NewRequest(http.MethodPost, url, reqBody)
8186
if err != nil {
@@ -91,6 +96,10 @@ func (bot *Bot) doPost(url string, reqHeader map[string]string, reqBody io.Reade
9196
}
9297
defer res.Body.Close()
9398

99+
if !isSuccess(res.StatusCode) {
100+
return fmt.Errorf("server response abnormality: %d", res.StatusCode)
101+
}
102+
94103
return json.NewDecoder(res.Body).Decode(resData)
95104
}
96105

0 commit comments

Comments
 (0)