dounai7148 2019-07-09 16:03
浏览 30
已采纳

从邮递员获取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

  • 写回答

1条回答 默认 最新

  • dpa55065 2019-07-09 17:58
    关注

    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())
        } 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图