I'm building microservices app with go and beego. I'm trying to pass JSON response from service A to service B as following:
func (u *ServiceController) GetAll() {
req := httplib.Get("http://localhost/api/1/services")
str, err := req.String()
// str = {"id":1, "name":"some service"}
if err != nil {
fmt.Println(err)
}
u.Data["json"] = str
u.ServeJSON()
}
However, when I send the response I actually double json encoding:
"{\"id\":\"1\",\"name\":\"some service\"}"
Finally, this is the solution I came up with:
func (u *ServiceController) GetAll() {
req := httplib.Get("http://localhost/api/1/services")
str, err := req.String()
if err != nil {
fmt.Println(err)
}
strToByte := []byte(str)
u.Ctx.Output.Header("Content-Type", "application/json")
u.Ctx.Output.Body(strToByte)
}