Skip to content

Instantly share code, notes, and snippets.

Last active January 4, 2023 15:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
package main
import (
func handleNoCors(w http.ResponseWriter, req *http.Request) {
fmt.Println("Request received. Referer:", req.Header.Get("Referer"))
w.Write([]byte("Hello world!"))
func handleCors(w http.ResponseWriter, req *http.Request) {
fmt.Println("[/cors] Request received. Referer:", req.Header.Get("Referer"))
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "")
func handleCorsRedirection(w http.ResponseWriter, req *http.Request) {
fmt.Println("[/cors-redirect] Request received. Referer:", req.Header.Get("Referer"))
w.Header().Set("Access-Control-Allow-Origin", "")
w.Header().Set("Location", "/cors")
func handleNoCorsRedirection(w http.ResponseWriter, req *http.Request) {
fmt.Println("[/no-cors-redirect] Request received. Referer:", req.Header.Get("Referer"))
w.Header().Set("Location", "/cors")
func main() {
http.HandleFunc("/cors", handleCors)
http.HandleFunc("/nocors", handleNoCors)
http.HandleFunc("/cors-redirect", handleCorsRedirection)
http.HandleFunc("/no-cors-redirect", handleNoCorsRedirection)
http.ListenAndServe(":3000", nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment