this returns the content type.. and that might be text/html; encoding=utf-8 ...
wrong...
context.getRequestContentType() should be marked as deprecated... or (may be better) documented in a better way... it simply returns the request content type including encoding...
Solution: We have to parse the "Accept" header (case insensitive).
One idea. Play 1 uses the following code that only allows html json xml as content types. Opinionated but makes parsing of the Accept header simple. Source code below.
But there might be better ways to solve this as Ninja potentially allows to support custom content types as well.
/**
* Automatically resolve request format from the Accept header
* (in this order : html > xml > json > text)
*/
public void resolveFormat() {
if (format != null) {
return;
}
if (headers.get("accept") == null) {
format = "html".intern();
return;
}
String accept = headers.get("accept").value();
if (accept.indexOf("application/xhtml") != -1 || accept.indexOf("text/html") != -1 || accept.startsWith("*/*")) {
format = "html".intern();
return;
}
if (accept.indexOf("application/xml") != -1 || accept.indexOf("text/xml") != -1) {
format = "xml".intern();
return;
}
if (accept.indexOf("text/plain") != -1) {
format = "txt".intern();
return;
}
if (accept.indexOf("application/json") != -1 || accept.indexOf("text/javascript") != -1) {
format = "json".intern();
return;
}
if (accept.endsWith("*/*")) {
format = "html".intern();
return;
}
}