0
votes

I want to know if there exists any way to identify with the Play Framework (with Java language not Scala) if the request is coming from JSON or HTML.

I find a method that is:

If (request().accepts(MimeType.HTML)){
    //code 
} else{
    //code
}

The only way that this works is that the JSON only accepts application/json if not will accept first if clause.

Want I want is that Play identify the Content-Type and not the Accept header.

1
if ( "application/json".equals(request().getHeader("Content-Type")) ){}? - Salem
@Salem write an answer instead of comment, especially if it's valid - biesior
Hi Salem for your answer, but i am getting[ NullPointerException: null] when i am sending a GET request with my browser (CHROME) - Aleyango

1 Answers

0
votes

You can check that the request body is parsable JSON without looking at the header manually.

public static Result someApi() {
    // Check if the body is parsable json
    JsonNode json = request().body().asJson();
    if (json == null) {
        // The request didn't have a json body
        return ok("not json");
    }

    // Do something with the json
    return ok("got json");
}