Created
November 7, 2017 17:02
-
-
Save Ynn/ea042ae5703aa74d8201d722f7fe3cd1 to your computer and use it in GitHub Desktop.
xml to json proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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