When the status code is explicitly set, the Content-Type header is automatically change to text/plain. I would like the ability to send back JSON data for API consistency.
func (u *UserController) Post() {
u.Ctx.ResponseWriter.WriteHeader(500)
u.Data["json"] = map[string]string{"error": "A user with that username already exists"}
fmt.Println(err)
u.ServeJson()
}
The response looks correct at first glance, it is formatted as a JSON object, but the Content-Type header is set to text/plain, limiting its ability to be used correctly.
func (u *UserController) GetAll() {
users := models.GetAllUsers()
u.Ctx.Output.SetStatus(300)
u.Data["json"] = users
u.ServeJson()
}

Thank you. What is the purpose of the Controller.Ctx.ResponseWriter.WriteHeader function if the Controller.Ctx.Output.SetStatus works with no side effects?
Controller.Ctx.ResponseWriter.WriteHeader will directly write to the client,
while Controller.Ctx.Output.SetStatus will cache the status until output the content
Thanks
Most helpful comment