码迷,mamicode.com
首页 > 其他好文 > 详细

go中处理各种请求方式以及处理接口请求参数

时间:2019-12-09 19:18:49      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:cat   listen   zha   fat   net   处理   imp   hello   ade   

话不多说直接上代码,解读内容全部在代码中

1、处理请求方式

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

//这个文件是对于net/http包的解读

func httpGet() {
	//func Get(url string) (resp *Response, err error)
	resp, err := http.Get("http://localhost:5379/ParseForm?id=1&name=zhangsan")//这里直接调用了http里面的get函数,传入对应url即可 返回的是http包中的respoonse类型
	if err != nil {
		// handle error
	}

	defer resp.Body.Close() //一定要关闭返回的response中的body
	body, err := ioutil.ReadAll(resp.Body) //读取body中的信息
	if err != nil {
		// handle error
	}

	fmt.Println(string(body))
}

func httpPost() {
	//func Post(url string, bodyType string, body io.Reader) (resp *Response, err error)
	resp, err := http.Post("http://localhost:5379/FormValue",
		"application/x-www-form-urlencoded",
		strings.NewReader("name=cjb")) //这里的第二个参数是传入参数的类型,第三个参数固定类型为io.Reader类型,因此调用了strings包中的func NewReader(s string) *Reader 转化为io.Reader类型
	if err != nil {
		fmt.Println(err)
	}

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

	fmt.Println(string(body))
}

func httpPostForm() {
	//func PostForm(url string, data url.Values) (resp *Response, err error)
	resp, err := http.PostForm("http://localhost:5379/hello",
		url.Values{"key": {"Value"}, "id": {"123"}}) //第二个参数规定的类型是url包中的Values类型 type Values map[string][]string

	if err != nil {
		// handle error
	}

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

	fmt.Println(string(body))

}

//这里一般是处理复杂的请求,比如要设置请求头以及一些请求信息时调用
func httpDo() {

	client := &http.Client{} //实例化client结构体

	//func NewRequest(method, urlStr string, body io.Reader) (*Request, error) //第一个是请求方法,第二个是请求地址,第三个是请求的参数,这里依旧调用了string保重的对应方法转化为对应得数据类型
	req, err := http.NewRequest("POST", "http://localhost:5379/hello", strings.NewReader("name=cjb"))
	if err != nil {
		// handle error
	}

	//设置请求投信息
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Set("Cookie", "name=anny")

	resp, err := client.Do(req)

	defer resp.Body.Close()

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

	fmt.Println(string(body))
}

func main() {
	httpGet()
	httpPost()
	// httpPostForm()
	// httpDo()
}

  2、处理接口请求参数

package main 
import(
	"fmt"
	"net/http"
	"log"
	"io"
)

//这里是对解析接口请求参数进行解析
func ParseForm(w http.ResponseWriter,r *http.Request){
	//http包中的Request类型中有一个字段是Form这个字段只有调用了 ParseForm()函数才会起作用
	//Form这个字段的类型是url.Values  也就是这个type Values map[string][]string
	err:= r.ParseForm() //func (r *Request) ParseForm() error 这个函数将解析URL中的查询字符串,并将解析结果更新到r.Form字段 
	if err!=nil{
		log.Fatal(err)
	}
	for i,v :=range r.Form{
		fmt.Fprintln(w,i,v)

	}
}

func FormValue(w http.ResponseWriter,r *http.Request){
	//func (r *Request) FormValue(key string) string 调用该函数获取指定参数的值
	name := r.FormValue("name")
	str := "name:" + name
	io.WriteString(w,str)
}

//这个是获取文件上传方式的参数值
//func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error)  
//FormFile返回以key为键查询r.MultipartForm字段得到结果中的第一个文件和它的信息。
//如果必要,本函数会隐式调用ParseMultipartForm和ParseForm。查询失败会返回ErrMissingFile错误

//这个是多文件上传时调用的
//func (r *Request) MultipartReader() (*multipart.Reader, error)
//如果请求是multipart/form-data POST请求,MultipartReader返回一个multipart.Reader接口,否则返回nil和一个错误。
//使用本函数代替ParseMultipartForm,可以将r.Body作为流处理。
func main(){
	http.HandleFunc("/ParseForm",ParseForm)
	http.HandleFunc("/FormValue",FormValue)
	http.ListenAndServe(":5379",nil)
}

  

go中处理各种请求方式以及处理接口请求参数

标签:cat   listen   zha   fat   net   处理   imp   hello   ade   

原文地址:https://www.cnblogs.com/xhen/p/12012700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!