2017-02-17 96 views
1

this.input.get()不工作beego

1package main 
 

 
import (
 
\t "database/sql" 
 

 
\t "flag" 
 
\t "fmt" 
 

 
\t "github.com/astaxie/beego" 
 
) 
 

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

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

 
func main() { 
 

 
\t beego.Router("/", &MainController{}) 
 
\t beego.Run() 
 

 
}

和請求是如下 捲曲-X POST http://localhost:8080/ -d「{ 「用戶名」: 「管理員」, 「密碼」:」管理員「}」

請求後擊中beego服務器我試圖在請求訪問的用戶名,但其顯示空

和outpit是

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

回答

1
下面 類型用戶的struct { 用戶名字符串 密碼字符串 }

要訪問結構的給定值,在代碼中

聲明的結構值第一個字符應Capital.example給出下面。 類型用戶的struct { 用戶名字符串 密碼串 }

和JSON編碼和解碼使用 json.Unmarshal(),json.Marshal()函數。

+0

謝謝@ user2737515 – gophervk

+0

有同樣的問題,大寫提示工作的感謝! – Abu

0

您請求主體需要解組JSON數據,更看到here

e.g:

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