一.获取请求头
- 在浏览器地址栏中输入下面信息,这属于http请求的get方式,请求携带两个参数
http://localhost:8090/param?name=zhang&age=18
- 可以使用http.Request.Header获取请求(Request Headers信息)
package main
import "fmt"
import "net/http"
func param(res http.ResponseWriter, req *http.Request) {
fmt.Fprintln(res, "第一个")
header:=req.Header
fmt.Fprintln(res,"Header全部数据:",header)
var acc []string =header["Accept"]
for _,n:=range acc{
fmt.Fprintln(res,"Accepth内容:",n)
}
}
func main() {
server := http.Server{
Addr: "localhost:8090",
}
http.HandleFunc("/param", param)
server.ListenAndServe()
}
二. 获取请求参数
package main
import "fmt"
import "net/http"
func param(res http.ResponseWriter, req *http.Request) {
req.ParseForm()
fmt.Fprintln(res,req.Form)
name:=req.FormValue("name")
age:=req.FormValue("age")
fmt.Fprintln(res,name,age)
}
func main() {
server := http.Server{
Addr: "localhost:8090",
}
http.HandleFunc("/param", param)
server.ListenAndServe()
}