http请求封装

This commit is contained in:
Diva123456 2023-02-27 12:48:56 +08:00
parent e85cb5e292
commit 5df5ab1d87
1 changed files with 32 additions and 0 deletions

32
common/tool/http.go Normal file
View File

@ -0,0 +1,32 @@
package tool
import (
"io"
"log"
"net/http"
)
const (
GET = "GET"
PUT = "PUT"
POST = "POST"
DELETE = "DELETE"
)
func HttpClient(method string, url string, payload io.Reader, token string) ([]byte, error) {
request, err := http.NewRequest(method, url, payload)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("User-Agent", "API Explorer")
request.Header.Add("token", token)
client := &http.Client{}
res, err := client.Do(request)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
return body, err
}