go - 从邮递员获取http post请求中的参数
问题描述
我有一个 Go 服务器,但似乎无法从 POST 请求中获取服务器中的 POST(表单)参数列表
当我在 Body 选项卡中选择的选项是form-data
并且请求如下所示时,我从邮递员发送请求:
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. 我该怎么办?
这就是我尝试获取价值的方式:
fmt.Fprintln(w, req.FormValue("aaa"))
在此先感谢您的帮助
解决方案
当它是多部分时,您要么必须这样做:
req.ParseMultipartForm(0)
fmt.Println(req.FormValue("aaa"))
或者如果您不想将整个内容加载到内存中,您可以这样做:
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())
}
}
推荐阅读
- javascript - 一个包的打字稿代码如何被其他包使用?
- video - FFMPEG - 在区域中滚动文本
- python - Pexpect:无法设置 shell 提示符
- google-cloud-platform - 使用 Deployment Manager 创建 Cloud SQL - “前提条件检查失败。” 错误
- algorithm - 如何获得两种不同标记化的对齐方式?(例如 BERT 与 spaCy)
- python - 有什么方法可以改变多类分类问题中目标类的数量?
- wordpress - 如何在 wordpress 的菜单项上添加通知徽章?
- c++ - pybind11::error_already_set 在内存位置(异常)
- c# - Azure 函数 c# .net 抛出 502
- c# - 错误数据类型不包含电子邮件地址