Created
November 28, 2018 10:10
-
-
Save bussiere/56413e46bd32406b48669c89d201d38d to your computer and use it in GitHub Desktop.
Receive a json in golang
This file contains 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
func test(w http.ResponseWriter, r *http.Request) { | |
var test = Data{} | |
var reception = Data{} | |
test.Fruit = "Banana" | |
body, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
panic(err) | |
} | |
err = json.Unmarshal(body, &reception) | |
fmt.Println(reception.Fruit) | |
js, err := json.Marshal(test) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
w.Header().Set("Content-Type", "application/json") | |
w.Write(js) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment