28 lines
575 B
Go
Raw Normal View History

2025-01-21 16:05:34 +08:00
package util
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// PostJSON post json 数据请求
func PostJSON(uri string, obj any) ([]byte, error) {
marshal, err := json.Marshal(obj)
if err != nil {
return nil, err
}
response, err := http.Post(uri, "application/json;charset=utf-8", bytes.NewBuffer(marshal))
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode)
}
return io.ReadAll(response.Body)
}