Building a quick and dirty REST API with Ratpack script; can't figure out how to allow DELETE from all origins.
I've tried setting headers within delete, and using all (as in sample code.) Sending DELETE with curl, postman, everything always returns 405. Am I missing something simple?
@Grapes([
@Grab('io.ratpack:ratpack-groovy:1.6.1')
])
ratpack {
handlers {
all {
MutableHeaders headers = response.headers
headers.set("Access-Control-Allow-Origin", "*")
headers.set("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE")
next()
}
post("product") {
...
}
get("product/:id") {
...
}
delete("product/:productId") {
// always returns 405
...
}
}
}
You see
HTTP/1.1 405 Method Not Allowedresponse status because your request gets handled by theget("product/:id")handler. If you want to use the same path for multiple HTTP methods, you can useprefixcombined withbyMethodmethod to define multiple handlers for the same path.Consider the following example:
As you can see in the example above, you can nest prefixes. We can test it with the following curl requests:
If you are interested in more details, I have written a blog post some time ago with a similar example - https://e.printstacktrace.blog/using-the-same-prefix-with-different-http-methods-in-ratpack/