35 lines
779 B
Go
35 lines
779 B
Go
package curl
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// 接口请求
|
|
func ApiCall(url, method string, header map[string]string, body io.Reader, timeOut time.Duration) (rsp *http.Response, err error) {
|
|
method = strings.ToUpper(method)
|
|
if method != "GET" && method != "POST" && method != "PUT" && method != "DELETE" {
|
|
return nil, errors.New("invalid http method")
|
|
}
|
|
if url == "" {
|
|
return nil, errors.New("request url can`t be empty")
|
|
}
|
|
client := &http.Client{}
|
|
if timeOut <= 0 {
|
|
client.Timeout = time.Second * 15
|
|
} else {
|
|
client.Timeout = timeOut
|
|
}
|
|
requestHandle, err := http.NewRequest(method, url, body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range header {
|
|
requestHandle.Header.Set(k, v)
|
|
}
|
|
return client.Do(requestHandle)
|
|
}
|