在此代码中,如果我评论ParseForm()
调用请求按预期工作
package main
import (
"fmt"
"net/http"
"net/url"
"strings"
)
func main() {
v := make(url.Values)
v.Set("status", "yeah!")
request, error := http.NewRequest("POST", "http://httpbin.org/post", strings.NewReader(v.Encode()))
if error != nil {
fmt.Println(error)
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
err:=request.ParseForm()
if err != nil {
fmt.Println(err)
}
fmt.Println(request.Form["status"])
response, error := http.DefaultClient.Do(request)
if error != nil {
fmt.Println(error)
} else {
fmt.Println(response)
}
}
但如果我调用ParseForm(),身体就会被清除,我得到:
Post http://httpbin.org/post: http: Request.ContentLength=14 with Body length 0
就像身体长度已经耗尽。如何访问表单值?我需要创建一个请求的签名。还有其他方法(直接从参数直接创建签名?)
答案 0 :(得分:1)
使用 ParseForm 来读取请求,而不是设置 对于简单的POST,你可以这样做:
resp, err := http.PostForm("http://example.com/form", url.Values{"key": {"Value"}, "id": {"123"}})
http://golang.org/pkg/net/http/#Post
http://golang.org/pkg/net/http/#PostForm