Skip to content

Instantly share code, notes, and snippets.

@jsmsalt
Created September 16, 2019 14:46
Show Gist options
  • Save jsmsalt/f58e3c43f1f615dc7f4bbd89f31d7020 to your computer and use it in GitHub Desktop.
Save jsmsalt/f58e3c43f1f615dc7f4bbd89f31d7020 to your computer and use it in GitHub Desktop.
Spark Java CORS Fix
options("/*", (request, response) -> {
String accessControlRequestHeaders = request.headers("Access-Control-Request-Headers");
if (accessControlRequestHeaders != null) {
response.header("Access-Control-Allow-Headers", accessControlRequestHeaders);
}
String accessControlRequestMethod = request.headers("Access-Control-Request-Method");
if (accessControlRequestMethod != null) {
response.header("Access-Control-Allow-Methods", accessControlRequestMethod);
}
return "OK";
});
before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));
// Paths
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment