Created
August 16, 2011 06:53
-
-
Save softprops/1148568 to your computer and use it in GitHub Desktop.
unfiltered jsonp example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
libraryDependencies ++= Seq( | |
"net.databinder" %% "unfiltered-jetty" % "0.4.1", | |
"net.databinder" %% "unfiltered-filter" % "0.4.1", | |
"net.databinder" %% "unfiltered-json" % "0.4.1" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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