Created
September 10, 2013 10:41
-
-
Save clooth/6507702 to your computer and use it in GitHub Desktop.
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 ( | |
| "encoding/json" | |
| "log" | |
| "net/http" | |
| "strconv" | |
| ) | |
| func writeJson(rw http.ResponseWriter, v interface{}) { | |
| // avoid json vulnerabilities, always wrap v in an object literal | |
| doc := map[string]interface{}{"d": v} | |
| if data, err := json.Marshal(doc); err != nil { | |
| log.Printf("Error marshalling json: %v", err) | |
| } else { | |
| rw.Header().Set("Content-Length", strconv.Itoa(len(data))) | |
| rw.Header().Set("Content-Type", "application/json") | |
| rw.Write(data) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment