Skip to content

Instantly share code, notes, and snippets.

@Ynn
Created November 7, 2017 17:02
Show Gist options
  • Save Ynn/ea042ae5703aa74d8201d722f7fe3cd1 to your computer and use it in GitHub Desktop.
Save Ynn/ea042ae5703aa74d8201d722f7fe3cd1 to your computer and use it in GitHub Desktop.
xml to json proxy
package main
import (
"fmt"
"strings"
xj "github.com/basgys/goxml2json"
"net/http"
"io/ioutil"
)
func handler(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get("http://"+r.URL.Path[1:])
if(err!=nil){
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// xml is an io.Reader
xml := strings.NewReader(string(body))
json, err := xj.Convert(xml)
if err != nil {
panic("That's embarrassing...")
}
fmt.Fprintf(w, "%s",json.String())
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment