vendredi 17 avril 2015

Submit Form as JSON with Play Framework

I am trying to submit a form as a JSON object because I want to create a REST API with play.


The issue that I have is that Play tells me that is not a valid JSON.


My FORM code:



@(form : Form[Product]) @main("Create Form"){
@helper.form(routes.Products.createProduct, 'enctype -> "application/json"){
@helper.inputText(form("name"))
<button>Commit</button>
} }


Controller Code:



// Read JSON an tell if it has a name Path
@BodyParser.Of(BodyParser.TolerantJson.class)
public static Result createProduct() {
JsonNode json = request().body().asJson();
String name = json.findPath("name").textValue();
if (name == null) {
return badRequest("Not JSON");
} else {
return ok(name);
}
}


Whats the best way to do this? a read about submitting with Ajax but because I am new with play I don´t figure it out the way to do this with Play´s form syntax.


Aucun commentaire:

Enregistrer un commentaire