go作为21世纪的新C语言,还是应该学习一下的,本文主要介绍net/http包相关用法
var client = http.Client{
Timeout: 10 * time.Second,
}
func HttpGetRequest(url string, result interface{}) error {
resp, err := client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&result)
return err
}
安全性
,且传输数据受URL
最大长度影响var client = http.Client{
Timeout: 10 * time.Second,
}
func HttpPostJson(url string, data interface{}, result interface{}, header map[string]string) error {
buf := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buf)
if err := encoder.Encode(data); err != nil {
return err
}
request, err := http.NewRequest(http.MethodPost, url, buf)
if err != nil {
return err
}
request.Header.Add("Content-Type", "application/json")
if header != nil {
for k, v := range header {
request.Header.Add(k, v)
}
}
response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
decoder := json.NewDecoder(response.Body)
if err = decoder.Decode(&result); err != nil {
return err
}
return nil
}
func HttpPostForm(posturl string, data url.Values, result interface{}, host string) error {
request, err := http.NewRequest(http.MethodPost, posturl, strings.NewReader(data.Encode()))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
//request.Host = host
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
if err = decoder.Decode(&result); err != nil {
return err
}
return nil
}
//Get get请求url参数
func Get(req *http.Request) map[string]string {
var result = make(map[string]string)
keys := req.URL.Query()
for k, v := range keys {
result[k] = v[0]
}
return result
}
数组
,因为go可以接收id=1&id=2这样形式的参数并放到同一个key下(但实际上应该没人这么传吧= =)//PostForm 获取postform形式的参数
func PostForm(req *http.Request) map[string]string {
//body, _ := ioutil.ReadAll(req.Body)
var result = make(map[string]string)
req.ParseForm()
for k, v := range req.PostForm {
if len(v) < 1 {
continue
}
result[k] = v[0]
}
return result
}
//PostJson 获取post json参数
func PostJson(req *http.Request, obj interface{}) error {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, obj)
if err != nil {
return err
}
return nil
}
var client = &http.Client{
Timeout: 10 * time.Second,
}
//DoHttpRequest 发起http请求并返回json结构体
func DoHttpRequest(method string, requestUrl string, reqBody io.Reader, obj interface{}, header map[string]string) error {
var req *http.Request
var err error
//find method to do
switch method {
case "GET":
fallthrough
case "get":
req, err = http.NewRequest(http.MethodGet, requestUrl, reqBody)
if err != nil {
return err
}
case "post":
fallthrough
case "POST":
fallthrough
case "post_form":
req, err = http.NewRequest(http.MethodPost, requestUrl, reqBody)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
case "post_json":
req, err = http.NewRequest(http.MethodPost, requestUrl, reqBody)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
default:
req, err = http.NewRequest(http.MethodGet, requestUrl, reqBody)
if err != nil {
return err
}
}
//set default host
if header == nil || header["host"] == "" {
req.Host = tools.GetUrlHost(requestUrl)
}
//add header
for k, v := range header {
if k == "host" {
req.Host = v
} else {
req.Header.Set(k, v)
}
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
//统一收集请求接口日志
//Do something
err = json.Unmarshal(respBody, &obj)
if err != nil {
return err
}
return nil
}
自从换工作后好久没写博客了,以后还是需要坚持下来,不仅仅是记录笔记,也是对自己过去工作的总结和回顾,但加班狗实在是没时间组织言辞了,填上的自己包里的代码凑活看吧 T.T