I am using Golang to write web applications, and I use beego framework. It seems the the framework have the internal status code returned for golang web server.
I am wondering is there any method in golang or beego, or other tools that can let me control the status code returned to browser, say 200, or 500 or others number.
Have a look on http.ResponseWriter.WriteHeader
. If you have access to the ResponseWriter
-object, you can easily return your own HTTP status code.
In your controller you can access the http.ResponseWriter
via the Ctx
type SomeController struct {
beego.Controller
}
func (c *SomeController) Get() {
c.Ctx.ResponseWriter.WriteHeader(500)
}
Edit: After further inspection you should probably do this:
func (c *SomeController) Get() {
c.CustomAbort(500, "Internal server error")
}
I'm leaving the reference about the context because you can find the http.Request
and http.ResponseWriter
on controller's Ctx
property.
c.Abort("500")
it response HTTP code is 200.
Must be:
c.Ctx.ResponseWriter.WriteHeader(500)