headers

Reading Play Framework HTTP request headers (examples) alvin April 26, 2018 - 11:46am

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.

How to use `curl` to get headers from a URL

Curl FAQ: How do I use curl to get the headers from a website URL?

Short answer: Use curl's -I option, like this:

$ curl -I URL

Here's a specific example, including a real URL and results:

How to set HTTP headers when sending a web service request alvin June 20, 2015 - 5:42pm

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 access HTTP response headers after making an HTTP request with Apache HttpClient alvin June 20, 2015 - 5:41pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a very short recipe, Recipe 15.12, “How to access HTTP response headers after making an HTTP request with Apache HttpClient.”

Problem

You need to access the HTTP response headers after making an HTTP request in your Scala code.

Solution

Use the Apache HttpClient library, and get the headers from the HttpResponse object after making a request:

How to send JSON POST data to a REST URL in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.11, “How to send JSON POST data to a REST URL.”

Problem

When writing Scala code, you want to send JSON data (or other data) to a POST URL, either from a standalone client, or when using a framework that doesn’t provide this type of service.

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:

How to view HTTP headers from the command line using curl

I’ve been working a lot on the next generation web server for the alvinalexander.com website lately, and as I’m looking at different HTTP accelerators, I keep wanting/needing to look at the headers returned by my web pages. A simple way to look at the HTTP headers from the command line is with the curl command, like this:

curl -I http://example.com/

Running this command against the main Google website, I see output like this: