scalatra

How to access POST request data with Scalatra

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.8, “How to access POST request data with Scalatra.”

Problem

You want to write a Scalatra web service method to handle POST data, such as handling JSON data sent as a POST request.

Solution

To handle a POST request, write a post method in your Scalatra servlet, specifying the URI the method should listen at:

How to access Scalatra web service GET parameters

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.7, “How to access Scalatra web service GET parameters.”

Problem

When creating a Scalatra web service, you want to be able to handle parameters that are passed into a method using a GET request.

Solution

If you want to let parameters be passed into your Scalatra servlet with a URI that uses traditional ? and & characters to separate data elements, like this:

How to replace XML servlet mappings with Scalatra mounts

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.6, “How to replace XML servlet mappings with Scalatra mounts.”

Problem

You want to add new servlets to your Scalatra application, and need to know how to add them, including defining their URI namespace.

How to create simple web services with Scalatra

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.5, “How to create simple web services with Scalatra.”

Problem

You want to be able to build new web services with Scalatra, a lightweight Scala web framework similar to the Ruby Sinatra library.

Solution

The recommended approach to create a new Scalatra project is to use Giter8, a great tool for building SBT directories for new projects.

Scala web service frameworks

I can never remember the name "unfiltered", so yesterday I kept trying to google for "web service framework", and to my surprise, Google wasn't showing unfiltered. In fact, it didn't show other tools like Scalatra, though it did show Play in the top ten.

So today's post is an effort to help Google out a little bit here. If you're looking for good Scala web service frameworks, check out these tools:

How to access Scalatra HTTP POST content (data) in post method

If you have a Scalatra POST method like this:

post("/foo") {
  // do stuff here
}

you can access the data that's sent to your POST function by using the implicit request object, like this:

val content = request.body

Then you can operate on that content as desired.