您的位置 首页 php

Golang编程 Golang封装http类,实现POST和GET调用

在golang中简单的http请求可以使用http.Get,http.Post和http.PostForm来发起,代码实现如下:

 //get请求
 resp, err := http.Get("")
 if err !=  nil  {
 // @todo
 }
 defer resp.Body. Close ()
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
 // @todo 
 }
 //post请求
 resp, err := http.Post("",
 "application/x-www-form-urlencoded",
 strings.NewReader("a=b"))
 if err != nil {
 @todo 
 }
 
 defer resp.Body.Close()
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
 // @todo 
 }

 //post表单请求
 resp, err := http.PostForm("",
 url.Values{"name": {"xiaoming"}, "id": {"9527"}})
 
 if err != nil {
 // @todo 
 }
 
 defer resp.Body.Close()
 body, err := ioutil.ReadAll(resp.Body)
 if err != nil {
 // @todo 
 } 


 

为了方便调用和和功能完善,我们对其进行封装到工具包中,如下:

golang编程

文章来源:智云一二三科技

文章标题:Golang编程 Golang封装http类,实现POST和GET调用

文章地址:https://www.zhihuclub.com/78582.shtml

关于作者: 智云科技

热门文章

网站地图