I'm using Play 2.2.1. I have the following route configuration in my route file:
OPTIONS /*path controllers.Application.options
GET / controllers.Application.index
...some more routes
I have the following set up in the Applications controller:
package controllers
import play.api.mvc._
object Application extends Controller {
def index = Action {
Ok(views.html.index())
}
def options = Action {
Ok("").withHeaders(
"Access-Control-Allow-Origin" -> "*",
"Access-Control-Allow-Methods" -> "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers" -> "Accept, Origin, Content-type, X-Json, X-Prototype-Version, X-Requested-With",
"Access-Control-Allow-Credentials" -> "true",
"Access-Control-Max-Age" -> (60 * 60 * 24).toString
)
}
}
When I try to test an OPTIONS request with curl, it gets completely ignored by play.
curl -X OPTIONS --include 'http://localhost:9000/foo/139'
I get this error back:
HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Content-Length: 7045
<!DOCTYPE html>
<html>
<head>
<title>Action not found</title>
...some more head junk
<body>
<h1>Action not found</h1>
<p id="detail">
For request 'OPTIONS /foo/139'
</p>
<h2>
These routes have been tried, in this order:
</h2>
<div>
<pre><span class="line">1</span><span class="route"><span class="verb">GET</span><span class="path">/</span><span class="call">controllers.Application.index</span></span></pre>
... more routes but none of them are for the OPTIONS request
What am I doing wrong here? Thanks in advance!
Do something with *path
segment - even if nothing...
routes:
OPTIONS / controllers.Application.options(path: String ?= "")
OPTIONS /*path controllers.Application.options(path)
Action:
def options(path: String) = Action {
Ok("").withHeaders(
"Access-Control-Allow-Origin" -> "*",
"Access-Control-Allow-Methods" -> "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers" -> "Accept, Origin, Content-type, X-Json, X-Prototype-Version, X-Requested-With",
"Access-Control-Allow-Credentials" -> "true",
"Access-Control-Max-Age" -> (60 * 60 * 24).toString
)
}
It works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With