1

I am writing a simple login/logout feature using Beego.

My init() in router.go file is given below:

func init() {
    beego.Router("/", &controllers.MainController{})
    beego.Router("/login", &controllers.AuthController{})
    beego.Router("/verify", &controllers.AuthController{}, "post:Verify")
}

In AuthController:

func (c *AuthController) Verify() {
    email := c.GetString("email")
    password := c.GetString("password")

    fmt.Printf("email: %v password: %v", email, password)
}

I just want to print the details to browser (for debugging purpose) and later redirect it to another page if the user is authenticated. But the issue here is that Beego always looks for a template file and throws the below error:

can't find templatefile in the path:views/authcontroller/verify.tpl

How can I stop Beego from acting like that or am I doing something that is "not-beego-like"?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Anoop S
  • 141
  • 1
  • 12
  • 1
    It sounds like Beego is looking for a template so it can return something to the client. What do you want it to return instead? – Jonathan Hall Sep 13 '19 at 09:11
  • Presumably it looks for a template because you haven't written a response in your action. – Peter Sep 13 '19 at 09:33
  • @Flimzy I just want to display the debug data (email, password etc) in browser. I am coming from php background and we can do that simply in php. I was trying to do the same here with Beego. – Anoop S Sep 13 '19 at 10:31
  • 1
    @AnoopS fmt.Printf prints to stdout, it doesn't write the response to the browser. If you want to display the data in the browser you first need to store it into the `Data` map and then serve it by calling one of the available `ServeXxx` methods ([example](https://beego.me/docs/mvc/controller/jsonxml.md#json%2C-xml-and-jsonp)). I don't use beego so I'm not sure whether there is a simpler way to do this. – mkopriva Sep 13 '19 at 11:37
  • 1
    So how do you want to display the debug data, if not by passing it to your template? – Jonathan Hall Sep 13 '19 at 12:01
  • 1
    @mkopriva Thank you for the comment. Now I pass the values to a template using `Data` and it works fine. – Anoop S Sep 13 '19 at 15:19

1 Answers1

0

if you don't set response type, beego is always looking for default template path.

if you don't want to render template you can set response type as;

func (c *AuthController) Verify() {
    defer c.ServerJSON() // response type
    email := c.GetString("email")
    password := c.GetString("password")

    fmt.Printf("email: %v password: %v", email, password)
}
ahmetlutfu
  • 325
  • 2
  • 9