mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-14 01:35:31 +00:00
26 lines
567 B
Go
26 lines
567 B
Go
package hook
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func post(url string, contentType string, body io.Reader) (string, error) {
|
|
r, err := http.Post(url, contentType, body)
|
|
if err != nil {
|
|
return "", fmt.Errorf("send request %v: %w", url, err)
|
|
}
|
|
if r.StatusCode == 204 {
|
|
return "", nil
|
|
} else if r.StatusCode != 200 {
|
|
return "", fmt.Errorf("unexpected status %v: %s", r.StatusCode, r.Status)
|
|
}
|
|
defer r.Body.Close()
|
|
bodyBytes, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read response: %w", err)
|
|
}
|
|
return string(bodyBytes), nil
|
|
}
|