怎么就不是原创了…
http.HandleFunc("/post", func(writer http.ResponseWriter, request *http.Request) {
// 检查是否POST请求
if request.Method != "POST" {
writer.WriteHeader(405)
return
}
// 解析form
err := request.ParseForm()
if err != nil {
writer.WriteHeader(400)
return
}
// 设置Content-Type
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(200)
data := map[string]interface{}{
"method": request.Method,
"url": request.RequestURI,
"header": request.Header,
// 包含URL里的查询参数
"query": request.URL.Query(),
// 只包含body里的参数
"postForm": request.PostForm,
// 包含URL和body里的参数
"form": request.Form,
}
json.NewEncoder(writer).Encode(data)
})