go中處理各種請求方式以及處理接口請求參數


話不多說直接上代碼,解讀內容全部在代碼中

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)
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM