Files
meowlib/http.go
T

45 lines
865 B
Go
Raw Normal View History

2024-03-23 20:09:14 +01:00
package meowlib
import (
"bytes"
"encoding/json"
"io"
"net/http"
2024-04-20 22:29:22 +02:00
"time"
2024-03-23 20:09:14 +01:00
)
func HttpGetId(url string) (response map[string]string, err error) {
srvId := make(map[string]string)
resp, err := http.Get(url + "/id")
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &srvId)
if err != nil {
return nil, err
}
return srvId, nil
}
2024-04-20 22:29:22 +02:00
func HttpPostMessage(url string, msg []byte, timeout int) (response []byte, err error) {
client := http.Client{
Timeout: time.Duration(timeout) * time.Second,
}
resp, err := client.Post(url+"/msg",
2024-03-23 20:09:14 +01:00
"application/octet-stream", bytes.NewBuffer(msg))
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}