英文:
this.input.get() not working beego
问题
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
1package main
import (
"database/sql"
"flag"
"fmt"
"github.com/astaxie/beego"
)
type User struct {
username string
password string
}type MainController struct {
beego.Controller
}
func (this *MainController) Post() {
this.Ctx.WriteString("hello world")
result := this.Input()
fmt.Println("eedwedwe", this.Input().Get("username"))
fmt.Println("input value is", result)
fmt.Println("eedwedwe", result.Get("Username"))
send := User{username: "vijay", password: "vk18"}
this.Data["json"] = &send
this.ServeJSON()
}
func main() {
beego.Router("/", &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[{"username" : "admin", "password":"admin"}:[]]
eedwedwe
<!-- end snippet -->
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
1package main
import (
"database/sql"
"flag"
"fmt"
"github.com/astaxie/beego"
)
type User struct {
username string
password string
}type MainController struct {
beego.Controller
}
func (this *MainController) Post() {
this.Ctx.WriteString("hello world")
result := this.Input()
fmt.Println("eedwedwe", this.Input().Get("username"))
fmt.Println("input value is", result)
fmt.Println("eedwedwe", result.Get("Username"))
send := User{username: "vijay", password: "vk18"}
this.Data["json"] = &send
this.ServeJSON()
}
func main() {
beego.Router("/", &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[{"username" : "admin", "password":"admin"}:[]]
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 := &User{}
err:= json.Unmarshal(this.Ctx.Input.RequestBody, u)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论