RequestBody does not work with Json

219 views
Skip to first unread message

Hannah J Swystun

unread,
Jul 18, 2016, 6:54:07 AM7/18/16
to play-framework
Hi,

I have a problem to handle a Json Request from BODY but it seems working with :
String[] computers = request().body().asFormUrlEncoded().get("computercode");

but not with :
JsonNode json = request().body().asJson();

Error returned (image below) , Json request is NULL!!! but it is not with asFormUrlEncoded!!!


Auto Generated Inline Image 1

Igmar Palsenberg

unread,
Jul 18, 2016, 6:58:18 AM7/18/16
to play-framework
Is the data posted with a application/json mime type ?


Igmar 

Hannah J Swystun

unread,
Jul 18, 2016, 7:12:44 AM7/18/16
to play-framework
I have no idea about mime type!!! I just followed the doc https://www.playframework.com/documentation/2.2.x/JavaJsonRequests
this is my code :

Auto Generated Inline Image 1

Hannah J Swystun

unread,
Jul 18, 2016, 7:25:05 AM7/18/16
to play-framework
Hi Igmar,
I added to my code this but this is not working yet:
response().setContentType("text/json");



Le lundi 18 juillet 2016 10:54:07 UTC, Hannah J Swystun a écrit :

Igmar Palsenberg

unread,
Jul 18, 2016, 7:39:13 AM7/18/16
to play-framework

 
I added to my code this but this is not working yet:
response().setContentType("text/json");

text/json is not application/json. Second, the request needs to set the proper header, modifying the response object is of no use (it is already parsed at that stage).

Igmar

Hannah J Swystun

unread,
Jul 18, 2016, 7:46:19 AM7/18/16
to play-framework
Igmar,

What are you suggesting me to do ? technically !


Le lundi 18 juillet 2016 10:54:07 UTC, Hannah J Swystun a écrit :

Igmar Palsenberg

unread,
Jul 18, 2016, 7:49:48 AM7/18/16
to play-framework
 
Igmar,

What are you suggesting me to do ? technically 

Send the correct mimetype with the POST ?


Ig,ar 

Hannah J Swystun

unread,
Jul 18, 2016, 8:36:11 AM7/18/16
to play-framework
I tried both of content type : Application & TEXT => KO

//Controller

@BodyParser.Of(BodyParser.Json.class)
public Result checkbox()
{
       
           
         
String[] postAction = request().body().asFormUrlEncoded().get("action");
         
         
String action = postAction[0];


 

   
String[] computers = request().body().asFormUrlEncoded().get("computercode");


               
    response
().setContentType("application/json");
   
//response().setContentType("text/json");

   
JsonNode json = request().body().asJson();
   
             
             
                 
String ids = ListUtils.mkString(computers, s -> "" + s, ";");
                 
Logger.info("This is working fine  : ids Selected are : "+ids);
           
                 
Logger.info("This returns NULL");    
                 
return ok("Json : "+json);
                       
             
}

list.scala.html
<form method="POST" action="/ComputersCustomActions">

<figure>
 
<button class="myBtn" type="submit" name="action" value="JsonList" formtarget="_blank"
           
style="background: url('@routes.Assets.at("images/toolbars/json.png")') ;">            
 
</button>
 
<figcaption>Json
         
<br>BindRequest
 
</figcaption>
</figure>

</form>

Routes :
POST        /ComputersCustomActions      controllers.HomeController.checkbox()



Le lundi 18 juillet 2016 10:54:07 UTC, Hannah J Swystun a écrit :

Igmar Palsenberg

unread,
Jul 18, 2016, 8:47:05 AM7/18/16
to play-framework


Op maandag 18 juli 2016 14:36:11 UTC+2 schreef Hannah J Swystun:
A normal POST will yield an application/x-www-form-urlencoded by default, not a JSON body. To get that, you'll have to use Javascript. You can't magically transform a normal body to JSON, you really need to send JSON.


Igmar
Reply all
Reply to author
Forward
0 new messages