Skip to content

Instantly share code, notes, and snippets.

@softprops
Created August 16, 2011 06:53
Show Gist options
  • Save softprops/1148568 to your computer and use it in GitHub Desktop.
Save softprops/1148568 to your computer and use it in GitHub Desktop.
unfiltered jsonp example
libraryDependencies ++= Seq(
"net.databinder" %% "unfiltered-jetty" % "0.4.1",
"net.databinder" %% "unfiltered-filter" % "0.4.1",
"net.databinder" %% "unfiltered-json" % "0.4.1"
)
$ curl -i 'http://localhost:3000/api'
HTTP/1.1 400 Bad Request
Content-Length: 32
Server: Jetty(7.2.2.v20101205)
Accept:application/json required
$ curl -i 'http://localhost:3000/api?callback=jsonpcallinback' --header 'Accept:application/json'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 34
Server: Jetty(7.2.2.v20101205)
jsonpcallinback({"hello":"world"})
$ curl -i 'http://localhost:3000/api' --header 'Accept:application/json'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 17
Server: Jetty(7.2.2.v20101205)
{"hello":"world"}
object Example {
import unfiltered.request._
import unfiltered.response._
def main(args: Array[String]) {
unfiltered.jetty.Http(3000).filter(unfiltered.filter.Planify{
case GET(Path("/api") & Jsonp.Optional(callback)) =>
// alternative to https://github.com/n8han/Unfiltered/blob/0.4.1/json/src/main/scala/response.scala
JsonContent ~> ResponseString(callback.wrap("""{"hello":"world"}"""))
case _ => BadRequest ~> ResponseString("Accept:application/json required")
}).run
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment