Skip to content

Instantly share code, notes, and snippets.

@76creates
Created March 26, 2020 12:06
Show Gist options
  • Save 76creates/3d07d336e9a3f5256ffe3de9b23d9790 to your computer and use it in GitHub Desktop.
Save 76creates/3d07d336e9a3f5256ffe3de9b23d9790 to your computer and use it in GitHub Desktop.
golang 302 redirection and order of things in response
// in order to do a redirection or any response including custom code/header/json body at the same time
// order in which they are arranged is important, one will overwrite another and you wont get proper
// response, bellow is the order which will allow you to set all 3
// any custom header goes first
w.Header().Set(
"Location",
"http://localhost/something/else
)
// then status code
w.WriteHeader(http.StatusFound)
// then json body
json.NewEncoder(w).Encode(resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment