go-web 获取get/post请求中的请求头和表单数据

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, "这是请求中的User-Agent信息:", r.Header["User-Agent"])
    fmt.Fprintln(w, "这是请求中的User-Agent信息:", r.Header.Get("User-Agent"))

    // 获取请求体内容的长度
    // len := r.ContentLength
    // body := make([]byte, len)
    // r.Body.Read(body)
    // fmt.Fprintln(w, "请求体中的内容是:", string(body))

    // 解析表单,在调用r.Form r.PostForm之前执行
    r.ParseForm()
    // fmt.Fprintln(w, "表单信息:", r.Form)
    fmt.Fprintln(w, "表单信息:", r.PostForm)

    // fmt.Fprintln(w, "用户名:", r.FormValue("username"))
    // fmt.Fprintln(w, "密码:", r.FormValue("password"))
    fmt.Fprintln(w, "密码:", r.PostFormValue("password"))

}

func main() {
    http.HandleFunc("/hello", handler)
    http.ListenAndServe(":8080", nil)

}

猜你喜欢

转载自www.cnblogs.com/Mishell/p/12303246.html