Felix
Felix

Reputation: 5629

java playframework read json from request body

I want to read a json from a request body.

my in body I have:

DefaultRequestBody(None,None,None,None,None,Some(MultipartFormData(Map(json -> List({"a":"s","b":"sd"})),List(),List(),List())))

Now I want to access the List ... what would be the best way in this case?

I have tried this:

 JsonNode json = request().body().asJson();

    if(json == null) {
        System.out.println("NULL");
        return badRequest("Expecting Json data");
    } else {
        String name = json.findPath("name").toString();
        if(name == null) {
            return badRequest("Missing parameter [name]");
        } else {
            return ok("Hello " + name);
        }
    }

but json is always null

thanks

Upvotes: 1

Views: 659

Answers (1)

Felix
Felix

Reputation: 5629

this workes for me

Http.RequestBody body = request().body();
JsonNode json = body.asJson();

System.out.println(json);

Upvotes: 1

Related Questions