0
点赞
收藏
分享

微信扫一扫

Golang_web_获取请求

Golang


文章目录

  • ​​Golang​​
  • ​​1.获取请求 URL​​
  • ​​2.获取请求头中的信息​​
  • ​​3.获取请求体中的信息​​
  • ​​4.获取请求参数​​
  • ​​4.1 Form 字段​​
  • ​​4.2 PostForm 字段​​
  • ​​4.3 MultipartForm​​


1.获取请求 URL

Request 结构中的 URL 字段用于表示请求行中包含的 URL,改字段是一个指向url.URL 结构的指针,让我们来看一下 URL 结构
Golang_web_获取请求_字段
main.go

package main
import (
"fmt"
"net/http"
)
//创建处理器函数
func handler(w http.ResponseWriter, r *http.Request){
fmt.Fprintln(w,"您输入的地址:",r.URL.Path)
fmt.Fprintln(w,"字符串:",r.URL.RawQuery)
}
func main() {
http.HandleFunc("/hello", handler)
http.ListenAndServe(":8080", nil)
}

Golang_web_获取请求_字段_02
Golang_web_获取请求_字段_03
Golang_web_获取请求_字段_04

2.获取请求头中的信息

通过 Request 结果中的 Header 字段用来获取请求头中的所有信息,Header 字段的类型是 Header 类型,而 Header 类型是一个 map[string][]string,string 类型的 key,string 切片类型的值。下面是 Header 类型及它的方法:

Golang_web_获取请求_请求参数_05
main.go

package main
import (
"fmt"
"net/http"
)
//创建处理器函数
func handler(w http.ResponseWriter, r *http.Request){
fmt.Fprintln(w,"您输入的地址:",r.URL.Path)
fmt.Fprintln(w,"字符串:",r.URL.RawQuery)
fmt.Fprintln(w,"头部信息全部为",r.Header)//获取头部信息
fmt.Fprintln(w,"头部信息中[Accept]的信息为:",r.Header["Accept"])
}
func main() {
http.HandleFunc("/hello", handler)
http.ListenAndServe(":8080", nil)
}

Golang_web_获取请求_字段_06

Golang_web_获取请求_字段_07
Golang_web_获取请求_字段_08

Get方法和[]的方法的区别
Golang_web_获取请求_请求参数_09

上面的为[]的方法,下面的为Get方法

3.获取请求体中的信息

请求和响应的主体都是有 Request 结构中的 Body 字段表示,这个字段的类型是io.ReadCloser 接口,该接口包含了 Reader 接口和 Closer 接口,Reader 接口拥有 Read方法,Closer 接口拥有 Close 方法

Golang_web_获取请求_字段_10

index.html







用户名: value="hanzong">

密 码 : value="666666">







main.go

package main
import (
"fmt"
"net/http"
)
//创建处理器函数
func handler(w http.ResponseWriter, r *http.Request){
fmt.Fprintln(w,"您输入的地址:",r.URL.Path)
fmt.Fprintln(w,"字符串:",r.URL.RawQuery)
fmt.Fprintln(w,"头部信息全部为",r.Header)
fmt.Fprintln(w,"头部信息中[Accept]的信息为:",r.Header["Accept"])
fmt.Fprintln(w,"头部信息中[Accept]的get信息为:",r.Header.Get("Accept"))
//获取内容的长度
length := r.ContentLength
//创建一个字节切片
body := make([]byte, length)
//读取请求体
r.Body.Read(body)
fmt.Fprintln(w, "请求体中的内容是:", string(body))
}
func main() {
http.HandleFunc("/hello", handler)
http.ListenAndServe(":8080", nil)
}

Golang_web_获取请求_请求参数_11
Golang_web_获取请求_字段_12
Golang_web_获取请求_表单_13

提交之后
Golang_web_获取请求_字段_14

4.获取请求参数

下面我们就通过 net/http 库中的 Request 结构的字段以及方法获取请求 URL 后面的请求参数以及 form 表单中提交的请求参数

4.1 Form 字段

类型是 url.Values 类型,Form 是解析好的表单数据,包括 URL 字段的 query参数和 POST 或 PUT 的表单数据。

Golang_web_获取请求_请求参数_15

Form 字段只有在调用 Request 的 ParseForm 方法后才有效。在客户端,会忽
略请求中的本字段而使用 Body 替代
Golang_web_获取请求_请求参数_16

获取username和password

函数

func handler(w http.ResponseWriter, r *http.Request) {
//解析表单
r.ParseForm()
//获取请求参数
fmt.Fprintln(w, "请求参数为:", r.Form)
}

需要在index.html加上如下的admin和pwd才可以在页面上显示出来password和username
Golang_web_获取请求_字段_17

Golang_web_获取请求_表单_18

4.2 PostForm 字段

类型也是 url.Values 类型,用来获取表单中的请求参数

func handler(w http.ResponseWriter, r *http.Request) {
//解析表单
r.ParseForm()
//获取请求参数
fmt.Fprintln(w, "请求参数为:", r.PostForm) }

结果

请求参数为: map[username:[hanzong] password:[666666]]

但是 PostForm 字段只支持 application/x-www-form-urlencoded 编码,如果form 表单的 enctype 属性值为 multipart/form-data,那么使用 PostForm 字段无法获取表单中的数据,此时需要使用 MultipartForm 字段

4.3 MultipartForm

为了取得 multipart/form-data 编码的表单数据,我们需要用到 Request 结构的ParseMultipartForm 方法和 MultipartForm 字段,我们通常上传文件时会将 form 表单的enctype 属性值设置为 multipart/form-data

表单

enctype="multipart/form-data">
用 户 名 : value="hanzong">

文件:



后台处理请求代码


package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
//解析表单
r.ParseMultipartForm(1024)
//打印表单数据
fmt.Fprintln(w, r.MultipartForm)
}
func main() {
http.HandleFunc("/upload", handler)
http.ListenAndServe(":8080", nil)
}

浏览器显示结果

&{map[username:[hanzong]] map[photo:[0xc042126000]]}

举报

相关推荐

0 条评论