1.获取GET参数 1.1 方法列表
方法名
描述
(r *Request) ParseForm() error
判断是否解析传参时出错
(r *Request) FormValue(key string) string
接收指定key的参数值
1.2 使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package mainimport ( "fmt" "net/http" ) func main () { http.HandleFunc("/login" , login) _ = http.ListenAndServe(":8888" , nil ) } func login (w http.ResponseWriter, r *http.Request) { if r.Method == "GET" && r.ParseForm() == nil { userName := r.FormValue("userName" ) fmt.Printf("userName: %s \n" , userName) passWord := r.FormValue("passWord" ) fmt.Printf("passWord: %s \n" , passWord) if userName == "" || passWord == "" { w.Write([]byte ("用户名或密码不能为空" )) } if userName == "admin" && passWord == "1234" { w.Write([]byte ("登录成功!" )) } else { w.Write([]byte ("用户名或密码错误!" )) } } }
2.获取POST参数 2.1 接收POST参数分以下两种情况
普通的Post表单: Content-Type=application/x-www-form-urlencoded
有文件上传的Post表单: Content-Type=multipart/form-data
2.2 普通的Post表单(r.PostFormValue) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package mainimport ( "fmt" "net/http" ) func main () { http.HandleFunc("/login" , login) _ = http.ListenAndServe(":8888" , nil ) } func login (w http.ResponseWriter, r *http.Request) { if r.Method == "POST" && r.ParseForm() == nil { userName := r.PostFormValue("userName" ) fmt.Printf("userName: %s \n" , userName) passWord := r.PostFormValue("passWord" ) fmt.Printf("passWord: %s \n" , passWord) if userName == "" || passWord == "" { w.Write([]byte ("用户名或密码不能为空" )) return } if userName == "admin" && passWord == "1234" { w.Write([]byte ("登录成功!" )) return } w.Write([]byte ("用户名或密码错误!" )) } else { w.Write([]byte ("当前接口,仅支持POST请求!" )) } return }
2.3 有文件上传的Post表单(r.FormFile) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 package mainimport ( "fmt" "io" "net/http" "os" "path" ) func main () { http.HandleFunc("/upload" , upload) _ = http.ListenAndServe(":8888" , nil ) } func upload (w http.ResponseWriter, r *http.Request) { if r.Method == "POST" && r.ParseForm() == nil { formFile, fileHeader, err := r.FormFile("file" ) if formFile == nil { http.Error(w,"上传的文件不能为空!" ,http.StatusInternalServerError) return } if err != nil { http.Error(w,err.Error(),http.StatusInternalServerError) return } defer formFile.Close() filename := fileHeader.Filename fmt.Printf("文件名称: %s \n" ,filename) ext := path.Ext(filename) fmt.Printf("文件后缀: %s \n" ,ext) pathName := "public/img" if !pathExist(pathName) { err := os.MkdirAll(pathName, os.ModePerm) if err != nil { http.Error(w,"创建目录失败: " + err.Error(),http.StatusInternalServerError) return } } newFile, err := os.Create(pathName +"/" + filename) if err != nil { http.Error(w,"创建文件失败: " + err.Error(),http.StatusInternalServerError) return } defer newFile.Close() written, err := io.Copy(newFile, formFile) fmt.Printf("上传结果: %d \n" ,written) if err != nil { http.Error(w,"文件上传失败: " + err.Error(),http.StatusInternalServerError) return } w.Write([]byte ("文件上传成功!" )) } return } func pathExist (path string ) bool { _, err := os.Stat(path) if err != nil { if os.IsExist(err) { return true } return false } return true }
运行结果
3.获取Cookie中的值 3.1 cookie的数据结构 Cookie
是一个结构体,其中有Cookie
的名和值、domain
、过期时间等信息,具体定义方式如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 type Cookie struct { Name string Value string Path string Domain string Expires time.Time RawExpires string MaxAge int Secure bool HttpOnly bool SameSite SameSite Raw string Unparsed []string }
3.2 使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package mainimport ( "encoding/base64" "fmt" "net/http" ) func main () { http.HandleFunc("/setCookie" , setCookie) http.HandleFunc("/getCookie" , getCookie) _ = http.ListenAndServe(":8888" , nil ) } func setCookie (w http.ResponseWriter, r *http.Request) { cookie1 := http.Cookie{ Name: "uid" , Value: "10001" , MaxAge: 3600 , } name := base64.URLEncoding.EncodeToString([]byte ("张三" )) cookie2 := http.Cookie{ Name: "name" , Value: name, MaxAge: 3600 , } http.SetCookie(w,&cookie1) http.SetCookie(w,&cookie2) w.Write([]byte ("cookie已设置!" )) } func getCookie (w http.ResponseWriter, r *http.Request) { key := r.FormValue("key" ) if key == "" { http.Error(w,"key不能为空!" ,http.StatusInternalServerError) return } cookie, err := r.Cookie(key) if err != nil { http.Error(w,"获取失败: " + err.Error(),http.StatusInternalServerError) return } result := make ([]string ,2 ) result[0 ] = cookie.Value decodeString, _ := base64.URLEncoding.DecodeString(cookie.Value) result[1 ] = string (decodeString) fmt.Println(result) w.Write([]byte ("cookie已获取!" )) }