You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
1.7 KiB
96 lines
1.7 KiB
1 month ago
|
package cutil
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
var client = http.Client{Timeout: 30 * time.Second}
|
||
|
|
||
|
func Get(url string) ([]byte, error) {
|
||
|
req, err := http.NewRequest("GET", url, nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
setAuth(req)
|
||
|
res, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
fmt.Println("request filter err:url==" + url + "||| err=" + err.Error())
|
||
|
return nil, err
|
||
|
}
|
||
|
body := res.Body
|
||
|
data, err := ioutil.ReadAll(body)
|
||
|
defer func() {
|
||
|
if body != nil {
|
||
|
err := body.Close()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return data, nil
|
||
|
}
|
||
|
|
||
|
func Put(url string, params map[string]interface{}) ([]byte, error) {
|
||
|
marshal, _ := json.Marshal(params)
|
||
|
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(marshal))
|
||
|
req.Header.Add("Accept", "application/json")
|
||
|
req.Header.Add("Content-Type", "application/json")
|
||
|
setAuth(req)
|
||
|
res, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
body := res.Body
|
||
|
data, err := ioutil.ReadAll(body)
|
||
|
defer func() {
|
||
|
if body != nil {
|
||
|
err := body.Close()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return data, nil
|
||
|
}
|
||
|
|
||
|
func Post(url string, params string) ([]byte, error) {
|
||
|
marshal := []byte(params)
|
||
|
req, err := http.NewRequest("POST", url, bytes.NewReader(marshal))
|
||
|
req.Header.Add("Accept", "application/json")
|
||
|
req.Header.Add("Content-Type", "application/json")
|
||
|
setAuth(req)
|
||
|
res, err := client.Do(req)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
body := res.Body
|
||
|
data, err := ioutil.ReadAll(body)
|
||
|
defer func() {
|
||
|
if body != nil {
|
||
|
err := body.Close()
|
||
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return data, nil
|
||
|
}
|
||
|
|
||
|
func setAuth(req *http.Request) {
|
||
|
|
||
|
}
|