wx-server/internal/otherapi/http_post.go

32 lines
621 B
Go
Raw Normal View History

2024-06-04 21:22:50 +08:00
package otherapi
import (
"bytes"
"encoding/json"
"io"
"net/http"
)
func http_post(url string, request, reply any) error {
jsonData, err := json.Marshal(request)
if err != nil {
return err
}
httpRequest, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonData))
if err != nil {
return err
}
httpRequest.Header.Set("Content-Type", "application/json")
client := &http.Client{}
response, err := client.Do(httpRequest)
if err != nil {
return err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
return json.Unmarshal(body, reply)
}