Go中http请求通用方式

在Go项目开发的过程当中http的请求方式挺多,不想去记忆,每次直接使用最基本的方式。json

// 复杂的请求参数
func HttpPost(url string) string {
	type Server struct {
		ServerName string
		ServerIp   string
		Age        int
	}

	type ServerSlice struct {
		Server    []Server
		ServersID string
	}
	//post 第三个参数是io.reader interface
	//strings.NewReader byte.NewReader bytes.NewBuffer 实现了read 方法
	s := ServerSlice{
		ServersID: "tearm",
		Server:    []Server{{"beijing", "127.0.0.1", 20}, {"shanghai", "127.0.0.1", 22}},
	}
	b, _ := json.Marshal(s) // 这里的s既能够是结构体,也能够是map[][]类型

	client := &http.Client{}
	// 若是存在参数,只需序列化成字符串,并转化为io.reader类型,传入第三个参数
	req, err := http.NewRequest("POST", url, strings.NewReader("heel="+string(b)))
	if err != nil {
		panic(err)
	}
	// 设置请求数据类型:"application/json","application/x-www-form-urlencoded"
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err := client.Do(req)

	if err != nil {
		// handle error
	}
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	return string(body)
}

func HttpGet(url string) string {
	//生成client 参数为默认
	client := &http.Client{}

	//第三个参数是io.reader interface
	//strings.NewReader byte.NewReader bytes.NewBuffer 实现了read 方法
	// 若是存在参数,只需序列化成字符串,并转化为io.reader类型,传入第三个参数
	// 举个例子:?name=pj&age=18 则传入参数能够为: strings.NewReader("name=pj&age=18")
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		// handle err
	}
	// 设置请求头
	// req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	//处理返回结果
	response, _ := client.Do(req)
	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		// handle err
	}
	return string(body)
}
复制代码
相关文章
相关标签/搜索