How to get the POST params in revel golang

huangapple go评论87阅读模式
英文:

How to get the POST params in revel golang

问题

如何在revel中获取POST请求的参数?我有以下代码:

func (c App) Ndc() revel.Result {
    fmt.Println(c.Params)
    //更简单的代码返回一个json...
}

我尝试了很多方法,但都没有成功,所以我希望尽可能保持代码的简洁。这是输出结果:

&{map[Origin:[LHR] Destination:[DME] DepartureDate:[2016-10-31] ArrivalDate:[]] map[] map[] map[] map[Origin:[LHR] Destination:[DME] DepartureDate:[2016-10-31] ArrivalDate:[]] map[] []}

内容是正确的,但是...我如何逐个获取这些变量及其值呢?
谢谢。

英文:

How can I get the parameters on a POST request in revel?? I have this:

func (c App) Ndc() revel.Result {
	fmt.Println(c.Params)
	//more simple code to return a json...
}

I have tested many things but nothing works, so I prefer let the code cleaner as possible. This is the output:

&{map[Origin:[LHR] Destination:[DME] DepartureDate:[2016-10-31] ArrivalDate:[]] map[] map[] map[] map[Origin:[LHR] Destination:[DME] DepartureDate:[2016-10-31] ArrivalDate:[]] map[] []}

The content is good, but... how can I get one by one these variables and their values??
Thank you.

答案1

得分: 0

这取决于你的请求中的Content-Type是否设置为application/json / text/json。如果是这样的话,你需要显式地将其转换为一个映射(map):

var j map[string]interface{}
c.Params.BindJSON(&j)
origin := j["Origin"] // 等等。

否则,以下代码应该可以工作:

origin := c.Params.Form["Origin"]
英文:

It depends on whether the Content-Type in your request is set to application/json / text/json. If so, you will need to explicitly convert it to a map:

var j map[string]interface{}
c.Params.BindJSON(&j)
origin := j["Origin"] // etc.

Otherwise this should work:

origin := c.Params.Form["Origin"]

答案2

得分: -1

这只是一个简单的映射,所有的值都在一个切片中。要从请求中获取Origin的值:

c.Params.Get("Origin")

**编辑:**上述方法不起作用,但这个应该可以:

c.Params.Get("Origin")
英文:

It's all just a simple map and all values are in an slice. To get the Origin value from the request:

<!-- language: go -->

c.Params[&quot;Origin&quot;][0]

EDIT: The above does not work, but this should:

<!-- language: go -->

c.Params.Get(&quot;Origin&quot;)

答案3

得分: -1

最后我找到了一个答案,不知道是否是最好的方法。按照这篇帖子的步骤,我找到了解决方案:

 fmt.Println(c.Request.Form["Origin"][0])

输出结果是LHR。

英文:

Finally I find an answer, I don't know if it is the best way. After follow step by step this post, I find the solution:

 fmt.Println(c.Request.Form[&quot;Origin&quot;][0])

The output is LHR.

huangapple
  • 本文由 发表于 2016年10月26日 17:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/40258729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定