原生的是用net/http发送请求,HttpRequest使这个操作简化了。
1.发送post请求,传参为json,见code:
1 import "github.cm/kirinlabs/HttpRequest" 2 3 var req=HttpRequest.NewRequest() 4 const BaseUrl="http://xxx" 5 6 func req(){ 7 8 req.SetHeaders(map[string]string){ 9 "Content-Type":"application/json" 10 } 11 req.SetCookies(map[string]string{ 12 "JESSIONID":"", 13 "currentUser":"", 14 } 15 res,err:=req.JSON().Post(BaseUrl,map[string]interface{}{ 16 "Name":"xx", 17 "Pwd":"xx", 18 }) 19 if nil != err{ 20 log.Fatal(err.Error()) 21 } 22 } 23 24 func main(){ 25 req() 26 }
2.发送post请求的其他几种方式:
res,_:=req.Post("url","Id=xx&Pwd=xx")
res,_:=req.Post("url",`{"Id":"xx","Pwd":"xx"}`)
res,_:=req.Post("utl","{"Id":"xx","Pwd":"xx"}")
3.发送get请求:
res,_:=req.Get("url"+"?conditionParam=xx")
本文转载自:https://blog.csdn.net/flyfreelyit/article/details/80281467#%E5%8F%91%E9%80%81%E8%AF%B7%E6%B1%82