从邮递员获取http发布请求中的参数[关闭]

从邮递员获取http发布请求中的参数[关闭]

问题描述:

I have a Go server but seems I can't get a list of POST (form) parameters in my server from the POST request

I send the request from postman when the option I selected in Body tab is form-data and the request looks like this:

POST /todo/323/item HTTP/1.1
Host: localhost:8080
Cache-Control: no-cache
Postman-Token: ef4b5606-3079-fb02-824f-f58ae89ee6f3
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="aaa"

skhdfb
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="test"

1213
------WebKitFormBoundary7MA4YWxkTrZu0gW--

I get null but when the option is x-www-form-urlencoded it works fine. What should I do?

this is how I try to get the value:

fmt.Fprintln(w, req.FormValue("aaa"))

thanks in advance for your help

我有一个Go服务器,但似乎无法从服务器获取服务器中的POST(表单)参数列表 POST请求 p>

当我在“正文”标签中选择的选项为 form-data code>并且该请求看起来像这样时,我从邮递员发送该请求: p>

  POST / todo / 323 / item HTTP / 1.1 
主机:localhost:8080 
缓存控制:无缓存
邮递员令牌:ef4b5606-3079-fb02-824f-f58ae89ee6f3 
内容 -类型:multipart / form-data;  boundary = ---- WebKitFormBoundary7MA4YWxkTrZu0gW 
 
 ------ WebKitFormBoundary7MA4YWxkTrZu0gW 
Content-Disposition:表单数据;  name =“ aaa” 
 
skhdfb 
 ------ WebKitFormBoundary7MA4YWxkTrZu0gW 
Content-Disposition:表单数据;  name =“ test” 
 
1213 
 ------ WebKitFormBoundary7MA4YWxkTrZu0gW-
  code>  pre> 
 
 

我得到null,但是当选项为 x- www-form-urlencoded code>,效果很好。 我该怎么办? p>

这是我尝试获取值的方式: p>

fmt.Fprintln(w,req.FormValue(“ aaa”)) code> p>

在此先感谢您的帮助 p> div>

When it's multipart you either have to do:

req.ParseMultipartForm(0)
fmt.Println(req.FormValue("aaa"))

or if you don't want to load the whole thing into memory, you could do:

form, err := req.MultipartReader()
for {
    part, err := form.NextPart()
    if err == io.EOF {
        break
    }
    if part.FormName() == "aaa" {
        buf := new(bytes.Buffer)
        buf.ReadFrom(part)
        fmt.Println(buf.String())
    } 
}