request

Reading Play Framework HTTP request headers (examples)

If you ever need to work with HTTP request headers in a Play Framework application, I hope the following examples will help. I was just looking at trying to access request headers like “User-Agent” and “Referer,” and ran a few tests.

Note: I put all of the Scala code that follows in Play Framework controller actions, then accessed the URL that was associated with those actions in the Play routes file, using the latest version of the Firefox browser on a MacOS system.

Scala: How to download and process XML data (such as an RSS feed)

I was looking for a good way to access XML resources (like RSS feeds) in Scala, and I currently like the idea of using ScalaJ-HTTP to access the URL and download the XML content, and then using the Scala XML library to process the XML string I download from the URL.

This example Scala program shows my current approach:

How to write a Play Framework POST request web service

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.15, “How to write a Play Framework POST request web service.”

Problem

You want to create a web service using the Play Framework that lets users send JSON data to the service using the POST request method.

Solution

Follow the steps from the previous recipe to create a new Play project, controller, and model.

How to write a GET request web service with the Play Framework

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.14, “How to write a GET request web service with the Play Framework.”

Problem

You want to create a GET request web service using the Play Framework, such as returning a JSON string when the web service URI is accessed.

Solution

When working with RESTful web services, you’ll typically be converting between one or more model objects and their JSON representation.

How to set HTTP headers when sending a web service request

Summary: This post is an excerpt from the Scala Cookbook, partially modified for the internet. This is a short recipe, Recipe 15.13, “How to set HTTP headers when sending a web service request.”

Problem

You need to set URL headers when making an HTTP request in Scala.

How to write a simple HTTP GET request client in Scala (with a timeout)

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.9, “How to write a simple HTTP GET request client in Scala.”

Back to top

Problem

You want an HTTP client you can use to make GET request calls.

Back to top

Solution

There are many potential solutions to this problem. This recipe demonstrates three approaches:

Table of Contents

  1. Problem
  2. Solution
Back to top

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 print content-type, headers, and body to debug a Play Framework controller

There are times when you’re debugging a Play Framework controller that you’ll want to print certain information, such as the request content-type, headers, content body, and query string. As a quick example, the code below shows how to print this information from a Play Framework controller method: