forked from juunini/simple-go-line-notify
/
send.go
48 lines (40 loc) · 946 Bytes
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package notify
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
const lineNotifyApiURL = "https://notify-api.line.me/api/notify"
func sendToLineServer(body io.Reader, accessToken, contentType string) (err error) {
req, err := http.NewRequest(
"POST",
lineNotifyApiURL,
body,
)
if err != nil {
return
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
res, err := client.Do(req)
if err != nil {
return
}
var responseBody struct {
Status int `json:"status"`
Message string `json:"message"`
}
if err = json.NewDecoder(res.Body).Decode(&responseBody); err != nil {
return
}
defer res.Body.Close()
if responseBody.Status != 200 {
err = fmt.Errorf("%d: %s", responseBody.Status, responseBody.Message)
}
return
}