Skip to content

Instantly share code, notes, and snippets.

@gregarndt
Created January 20, 2016 16:32
Show Gist options
  • Save gregarndt/93b72d9eec80e3ab616c to your computer and use it in GitHub Desktop.
Save gregarndt/93b72d9eec80e3ab616c to your computer and use it in GitHub Desktop.
raw message nil
func (self *Routes) ServeHTTP(res http.ResponseWriter, req *http.Request) {
payload := (*json.RawMessage)(nil)
if req.Body != nil {
body, err := ioutil.ReadAll(req.Body)
// If we fail to create a request notify the client.
if err != nil {
res.WriteHeader(500)
fmt.Fprintf(res, "Failed to generate request (could not read http body) - %s", err)
return
}
payload = new(json.RawMessage)
err = json.Unmarshal(body, payload)
if err != nil {
res.WriteHeader(400)
fmt.Fprintf(res, "Malformed payload - http request body is not valid json - %s", err)
return
}
}
// do something with payload
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment