1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-28 08:39:13 +02:00

fix: use custom http client to send request

This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ 2022-04-23 20:23:01 +08:00
parent c364220b47
commit 8fdd3c24dc

View File

@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/pkg/errors"
)
@ -61,17 +62,25 @@ func (p *Service) Send(ctx context.Context, subject, content string) error {
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, "POST", p.url, bytes.NewBuffer(data))
if err != nil {
return errors.Wrap(err, "failed to create bark request")
}
resp, err := http.Post(p.url, "application/json; charset=utf-8", bytes.NewReader(data))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
httpClient := &http.Client{
Timeout: time.Second * 5,
}
resp, err := httpClient.Do(req)
if err != nil {
return errors.Wrap(err, "send bark request failed")
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("statusCode: %d, body: %v", resp.StatusCode, string(result))
return errors.Wrap(err, "send bark message failed")