this.input.get() 在 beego 中不起作用。

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

this.input.get() not working beego

问题

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. 1package main
  2. import (
  3. &quot;database/sql&quot;
  4. &quot;flag&quot;
  5. &quot;fmt&quot;
  6. &quot;github.com/astaxie/beego&quot;
  7. )
  8. type User struct {
  9. username string
  10. password string
  11. }type MainController struct {
  12. beego.Controller
  13. }
  14. func (this *MainController) Post() {
  15. this.Ctx.WriteString(&quot;hello world&quot;)
  16. result := this.Input()
  17. fmt.Println(&quot;eedwedwe&quot;, this.Input().Get(&quot;username&quot;))
  18. fmt.Println(&quot;input value is&quot;, result)
  19. fmt.Println(&quot;eedwedwe&quot;, result.Get(&quot;Username&quot;))
  20. send := User{username: &quot;vijay&quot;, password: &quot;vk18&quot;}
  21. this.Data[&quot;json&quot;] = &amp;send
  22. this.ServeJSON()
  23. }
  24. func main() {
  25. beego.Router(&quot;/&quot;, &amp;MainController{})
  26. beego.Run()
  27. }

<!-- end snippet -->

请求如下所示:
curl -X POST http://localhost:8080/ -d '{"username" : "admin", "password":"admin"}'

在请求命中beego服务器后,我尝试访问请求中的用户名,但显示为空

输出如下所示:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. 2017/02/17 12:56:59 [I] [asm_amd64.s:2086] http server Running on http://:8080
  2. eedwedwe
  3. input value is map[{&quot;username&quot; : &quot;admin&quot;, &quot;password&quot;:&quot;admin&quot;}:[]]
  4. eedwedwe

<!-- end snippet -->

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. 1package main
  2. import (
  3. &quot;database/sql&quot;
  4. &quot;flag&quot;
  5. &quot;fmt&quot;
  6. &quot;github.com/astaxie/beego&quot;
  7. )
  8. type User struct {
  9. username string
  10. password string
  11. }type MainController struct {
  12. beego.Controller
  13. }
  14. func (this *MainController) Post() {
  15. this.Ctx.WriteString(&quot;hello world&quot;)
  16. result := this.Input()
  17. fmt.Println(&quot;eedwedwe&quot;, this.Input().Get(&quot;username&quot;))
  18. fmt.Println(&quot;input value is&quot;, result)
  19. fmt.Println(&quot;eedwedwe&quot;, result.Get(&quot;Username&quot;))
  20. send := User{username: &quot;vijay&quot;, password: &quot;vk18&quot;}
  21. this.Data[&quot;json&quot;] = &amp;send
  22. this.ServeJSON()
  23. }
  24. func main() {
  25. beego.Router(&quot;/&quot;, &amp;MainController{})
  26. beego.Run()
  27. }

<!-- end snippet -->

and request is as follows
curl -X POST http://localhost:8080/ -d '{"username" : "admin", "password":"admin"}'

after request has hit beego server i am trying to access username in the request but its showing empty

and the outpit is

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  1. 2017/02/17 12:56:59 [I] [asm_amd64.s:2086] http server Running on http://:8080
  2. eedwedwe
  3. input value is map[{&quot;username&quot; : &quot;admin&quot;, &quot;password&quot;:&quot;admin&quot;}:[]]
  4. eedwedwe

<!-- end snippet -->

答案1

得分: 2

在你提供的代码中声明了一个结构体(Structure):

  1. type User struct {
  2. username string
  3. password string
  4. }

要访问结构体的值,首字母应该大写,例如:

  1. type User struct {
  2. Username string
  3. Password string
  4. }

而在进行 JSON 编码和解码时,可以使用 json.Unmarshal()json.Marshal() 函数。

英文:

Declared Structure in your code given below
type User struct {
username string
password string
}

To access the values of Structure Values first character should be Capital.example given below.
type User struct {
Username string
Password string
}

And json encoding and decoding use
json.Unmarshal(), json.Marshal() function.

答案2

得分: 1

你需要从请求体中解析JSON数据,更多信息请参考这里

例如:

  1. u := &User{}
  2. err := json.Unmarshal(this.Ctx.Input.RequestBody, u)
英文:

you need unmarshal json data from request body , more see here

e.g:

  1. u := &amp;User{}
  2. err:= json.Unmarshal(this.Ctx.Input.RequestBody, u)

huangapple
  • 本文由 发表于 2017年2月17日 15:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/42291833.html
匿名

发表评论

匿名网友

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

确定