From d70c3e598450a71ae3971770f63d13dbf6838952 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 22:53:23 -0500 Subject: Add more CORS related header management `Access-Control-Allow-Methods` & `Access-Control-Allow-Headers` headers are needed to make POST requests from angular. --- backend/main.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/main.go b/backend/main.go index 0bd5ebf..001a5cb 100644 --- a/backend/main.go +++ b/backend/main.go @@ -93,6 +93,11 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { sendResponse(w, http.StatusCreated, "Recipe added successfully", recipe) + } else if r.Method == "OPTIONS" { + + w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS") //Enable CORS + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") //Enable CORS + sendResponse(w, http.StatusOK, "Set Allowed Methods CORS", nil) } else { sendResponse(w, http.StatusMethodNotAllowed, "Invalid method", nil) @@ -183,6 +188,11 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { "Recipe Deleted Successfully", nil) } + } else if r.Method == "OPTIONS" { + + w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") //Enable CORS + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") //Enable CORS + sendResponse(w, http.StatusOK, "Set Allowed Methods CORS", nil) } else { sendResponse(w, http.StatusMethodNotAllowed, "Invalid method", nil) -- cgit v1.1