【问题标题】:Mux handler not receiving all the query parameters passed from curl postMux 处理程序未接收到从 curl post 传递的所有查询参数
【发布时间】:2018-05-21 13:57:16
【问题描述】:

我无法使用 Mux 接收所有查询参数。只收到第一部分

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/resize", resizeImageFromPayload).Methods("POST")
    log.Fatal(http.ListenAndServe(":8080", router))
}

func resizeImageFromPayload(w http.ResponseWriter, r *http.Request) {
    widthParameter := r.URL.Query().Get("width")
    heightParameter := r.URL.Query().Get("height")
    fmt.Println(r.URL.String())
    fmt.Println(widthParameter)
    fmt.Println(heightParameter)
   //More code..
}

当我使用 curl curl -XPOST http://localhost:8080/resize?width=100&height=100 -o img_resize.png -F "file=@snap1.png" 调用 api 时,它会打印:

/resize?width=100
100

它似乎省略了 &height=100 部分。有什么想法吗?

提前致谢。

【问题讨论】:

  • 您的 shell 将 & 解释为您打算将 curl 命令分叉到后台。将您的 URL 用引号括起来以查看所需的输出。
  • 在 shell 中运行时,尝试将 URL 用单引号括起来。
  • 感谢它有效 :) 毕竟不相关
  • 这是 GET http 方法,而不是 POST

标签: go mux


【解决方案1】:

URL http://localhost:8080/resize?width=100&height=100 包含一个特殊字符 &,它具有 another meaning 到 shell。

为了在 URL 中使用 & 符号 (&) 作为实际字符,您需要将 URL 放在引号中:"http://localhost:8080/resize?width=100&height=100"

【讨论】:

  • 谢谢。这正是cmets中的家伙所说的。标记为已接受的答案。
猜你喜欢
  • 2015-03-25
  • 2015-12-18
  • 2020-11-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 2016-10-05
相关资源
最近更新 更多