1.无参数Get请求
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main(){
resp, err := http.Get("http://baidu.com/")
if err != nil {
// handle error
}
defer resp.Body.Close() //关闭body
body, err := ioutil.ReadAll(resp.Body)
if err != nil { //处理错误
// handle error
}
fmt.Println(string(body))
}
2.有参数Get请求
func main() {
apiUrl := "http://127.0.0.1:9090/get"
data := url.Values{}
data.Set("name", "xiaomi")
data.Set("age", "18")
//把string转换为url
u, err := url.ParseRequestURI(apiUrl)
if err != nil {
fmt.Println("parese url requestUrl failed,err:", err)
}
//URL encode
u.RawQuery = data.Encode()
fmt.Println(u.String())
resp, err := http.Get(u.String())
if err != nil {
fmt.Println("get failed, err:", err)
return
}
defer resp.Body.Close()
//读取内容
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("get resp failed, err:", err)
return
}
fmt.Println(string(b))
}
可以把拼接Url的部分单独领出来,变成:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"net/url"
)
func ConvertToQueryParams(requrl string ,params map[string]string) string {
data := url.Values{}
for key, value := range params {
data.Set(key, value)
}
u, _ := url.ParseRequestURI(requrl)
u.RawQuery = data.Encode()
return u.String()
}
func main{
Url := "http://127.0.0.1:9090/get"
params := map[string]string{"name":"xiaomi","age":"18"}
requrl := ConvertToQueryParams(Url,params)
resp, err := http.Get(requrl)
if err != nil {
fmt.Println("get failed, err:", err)
return
}
defer resp.Body.Close()
//读取内容
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("get resp failed, err:", err)
return
}
fmt.Println(string(b))
}
3.有body无参数Post请求
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func main() {
//Post请求示例
url := "http://127.0.0.1:9090/post"
// 表单数据
//contentType := "application/x-www-form-urlencoded"
//data := "name=枯藤&age=18"
// json
contentType := "application/json"
data := `{"name":"xiaomi","age":18}`
resp, err := http.Post(url, contentType, strings.NewReader(data))
if err != nil {
fmt.Println("post failed, err:", err)
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("get resp failed, err", err)
return
}
fmt.Println(string(b))
}
4.有body有参数Post请求(参数是指url内"?"后面的键值对)
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"net/url"
)
//依旧单独分出来Url拼接的部分
func ConvertToQueryParams(requrl string ,params map[string]string) string {
data := url.Values{}
for key, value := range params {
data.Set(key, value)
}
u, _ := url.ParseRequestURI(requrl)
u.RawQuery = data.Encode()
return u.String()
}
func main(){
//post请求带参数和body
appiurl := "http://127.0.0.1:9090/post"
params := map[string]string{"access_token":"d3cb174XXXXX",}
body := `{"name":"xiaomi","age":"18"}`
resp,_ := http.Post(ConvertToQueryParams(appiurl,params),"application/json",strings.NewReader(body))
b, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(b))
defer resp.Body.Close()
}
最后还可以吧读取返回的Body简单封装,看个人需要
package http
import (
"bytes"
"crypto/tls"
"io/ioutil"
"net/http"
"strings"
)
var httpClient = &http.Client{}
func init() {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient = &http.Client{Transport: tr}
}
//也可以返回成string,改写最后的return为string(b)
func ResponseHandle(resp *http.Response, err error) ([]byte, error) {
if err != nil {
return []byte(""), err
}
b, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return []byte(""), err
}
return b, nil
}
func XPost(url string, params map[string]string, body string) ([]byte, error) {
resp, err := httpClient.Post(ConvertToQueryParams(url,params), "application/json", strings.NewReader(body))
return ResponseHandle(resp, err)
}
func XGet(url string, params map[string]string) ([]byte, error) {
resp, err := httpClient.Get(ConvertToQueryParams(url,params))
return ResponseHandle(resp, err)
}