1 Star 0 Fork 0

linxing / youye-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
http.go 1.12 KB
一键复制 编辑 原始数据 按行查看 历史
Mark 提交于 2023-12-23 11:58 . init
package pkg
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"time"
)
// 发送GET请求
// url: 请求地址
// response: 请求返回的内容
func Get(url string) (string, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Accept", "*/*")
req.Header.Set("Content-Type", "application/json")
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
return string(result), nil
}
// 发送POST请求
// url: 请求地址
// data: POST请求提交的数据
// contentType: 请求体格式,如:application/json
// content: 请求放回的内容
func Post(url string, data interface{}, contentType string) ([]byte, error) {
// 超时时间:5秒
client := &http.Client{Timeout: 5 * time.Second}
jsonStr, _ := json.Marshal(data)
resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
if err != nil {
return nil, err
}
defer resp.Body.Close()
result, _ := ioutil.ReadAll(resp.Body)
return result, nil
}
1
https://gitee.com/linxing_3/youye-core.git
git@gitee.com:linxing_3/youye-core.git
linxing_3
youye-core
youye-core
v0.0.1-202404291803

搜索帮助