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

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

this.input.get() not working beego

问题

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

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

1package main

import (
	&quot;database/sql&quot;

	&quot;flag&quot;
	&quot;fmt&quot;

	&quot;github.com/astaxie/beego&quot;
)

type User struct {
	username string
	password string
}type MainController struct {
	beego.Controller
}

func (this *MainController) Post() {
	this.Ctx.WriteString(&quot;hello world&quot;)
	result := this.Input()
	fmt.Println(&quot;eedwedwe&quot;, this.Input().Get(&quot;username&quot;))
	fmt.Println(&quot;input value is&quot;, result)
	fmt.Println(&quot;eedwedwe&quot;, result.Get(&quot;Username&quot;))
	send := User{username: &quot;vijay&quot;, password: &quot;vk18&quot;}
	this.Data[&quot;json&quot;] = &amp;send
	this.ServeJSON()
}

func main() {

	beego.Router(&quot;/&quot;, &amp;MainController{})
	beego.Run()

}

<!-- 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 -->

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

<!-- end snippet -->

英文:

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

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

1package main

import (
	&quot;database/sql&quot;

	&quot;flag&quot;
	&quot;fmt&quot;

	&quot;github.com/astaxie/beego&quot;
)

type User struct {
	username string
	password string
}type MainController struct {
	beego.Controller
}

func (this *MainController) Post() {
	this.Ctx.WriteString(&quot;hello world&quot;)
	result := this.Input()
	fmt.Println(&quot;eedwedwe&quot;, this.Input().Get(&quot;username&quot;))
	fmt.Println(&quot;input value is&quot;, result)
	fmt.Println(&quot;eedwedwe&quot;, result.Get(&quot;Username&quot;))
	send := User{username: &quot;vijay&quot;, password: &quot;vk18&quot;}
	this.Data[&quot;json&quot;] = &amp;send
	this.ServeJSON()
}

func main() {

	beego.Router(&quot;/&quot;, &amp;MainController{})
	beego.Run()

}

<!-- 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 -->

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

<!-- end snippet -->

答案1

得分: 2

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

type User struct {
	username string
	password string
}

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

type User struct {
	Username string
	Password string
}

而在进行 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数据,更多信息请参考这里

例如:

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

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

e.g:

u := &amp;User{}
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:

确定