Is there a way to convert XML ([] byte) to JSON output in Golang?
I have a function below body []byte , but I want to convert this XML response to JSON after some manipulations. I tried Unmarshal in the xml package without success:
// POST func (u *UserResource) authenticateUser(request *restful.Request, response *restful.Response) { App := new(Api) App.url = "http://api.com/api" usr := new(User) err := request.ReadEntity(usr) if err != nil { response.AddHeader("Content-Type", "application/json") response.WriteErrorString(http.StatusInternalServerError, err.Error()) return } buf := []byte("<app version=\"1.0\"><request>1111</request></app>") r, err := http.Post(App.url, "text/plain", bytes.NewBuffer(buf)) if err != nil { response.AddHeader("Content-Type", "application/json") response.WriteErrorString(http.StatusInternalServerError, err.Error()) return } defer r.Body.Close() body, err := ioutil.ReadAll(r.Body) response.AddHeader("Content-Type", "application/json") response.WriteHeader(http.StatusCreated) // err = xml.Unmarshal(body, &usr) // if err != nil { // fmt.Printf("error: %v", err) // return // } response.Write(body) // fmt.Print(&usr.userName) }
I also use Go-restful package
source share