43 lines
797 B
Go
43 lines
797 B
Go
|
//
|
||
|
// http_helper.go
|
||
|
// Copyright (C) 2023 tiglog <me@tiglog.com>
|
||
|
//
|
||
|
// Distributed under terms of the MIT license.
|
||
|
//
|
||
|
|
||
|
package helper
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
func RequestJson(url string, data []byte) (*http.Response, error) {
|
||
|
res, err := http.Post(url, "application/json", bytes.NewBuffer(data))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|
||
|
|
||
|
func NotifyBack(url string, vals map[string]any, errmsg string) (*http.Response, error) {
|
||
|
if url == "" {
|
||
|
return nil, errors.New("no url")
|
||
|
}
|
||
|
vals["msg"] = errmsg
|
||
|
if errmsg == "" {
|
||
|
vals["stat"] = "ok"
|
||
|
} else {
|
||
|
vals["stat"] = "fail"
|
||
|
}
|
||
|
|
||
|
body, _ := json.Marshal(vals)
|
||
|
res, err := http.Post(url, "application/json", bytes.NewReader(body))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return res, nil
|
||
|
}
|