2

I am a scalatra newbie, and maybe my question makes no sense, but here it is:

How do I tell if a request in scalatra was a GET or a HEAD request?

Basically I have a REST api which uses GET to get an item, and HEAD to test that the item exists. I am not seeing an obvious way of writing a handler for a HEAD request.

Will I Am
  • 2,614
  • 3
  • 35
  • 61
  • What did you try? I never used Scalatra, but I suppose you should just implement `head()` in a similar way as you did with `get()` but with an empty response body... – Aurélien Bénel Jul 13 '13 at 10:13
  • I am not seeing an explicit head() specified in documentation, and I get an error if I try it. From reading around, a HEAD request gets handled by the get(), in which case I'd need to be able to tell them apart in the handler. Am I mistaken? – Will I Am Jul 13 '13 at 17:27

2 Answers2

2

Scalatra 2.2.2 (at least) implements HEAD by itself, running the GET code and dropping the information from the body.

KArvan
  • 43
  • 7
1

This seems to work. I am not sure if it's the right way, and it's not clean (scalatra should make HEAD a first class citizen). Anyway, hopefully someone will correct me if I'm doing something wrong. but this is my newbie attempt... Not exactly sure if case matters.

get ("/something",request.getMethod == "HEAD") {

}

get ("/something",request.getMethod == "GET") {

}
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
Will I Am
  • 2,614
  • 3
  • 35
  • 61
  • This seems like a decent way to do it to me, but I wonder if Scalatra has something up its sleeve or makes some assumptions somewhere. For example, see the documentation for `org.scalatra.RouteRegistry.apply` and `matchingMethods`: "HEAD must be identical to GET without a body, so HEAD returns GET's routes." – Michelle Tilley Jul 14 '13 at 06:55