1

I have a Play application with a POST route which will act as a RESTful API.

Whats the best way to get POST data within a controller? As you can see from my controller I have attempted this, however it doesn't appear to work correctly.

Routes:

# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET     /                           controllers.Application.index()
GET     /api/getMessages            controllers.Application.getMessages()
POST    /api/createMessage          controllers.Application.createMessages()

Controller:

package controllers;

import play.*;
import play.mvc.*;
import static play.libs.Json.toJson;

import java.util.Map;

import models.*;

import views.html.*;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }

    public static Result createMessages(){
        final Map<String, String[]> values = request().body().asFormUrlEncoded();

        String from = values.get("from")[0];
        String subject = values.get("subject")[0]; 
        String message = values.get("message")[0];

        Message.create(from, subject, message);

        return ok(toJson("ok"));
    }

    public static Result getMessages(){
        return ok(toJson(Message.all()));
    }

}

Request:

Request Url: http://localhost:9000/api/createMessage
Request Method: POST
Status Code: 400
Params: {
    "from": "[email protected]",
    "subject": "Hello",
    "message": "World"
}
2
  • 2
    What's the error message? Commented May 19, 2014 at 20:45
  • The error returned is null Commented May 20, 2014 at 8:23

2 Answers 2

2

Try with DynamicForm:

 public static Result createMessages(){
    DynamicForm df = play.data.Form.form().bindFromRequest();

    String from = df.get("from");
    String subject = df.get("subject");
    String message = df.get("message");

    if(from != null && subject != null && message != null){
        Message.create(from, subject, message);
        return ok(toJson("ok"));
    } else {
        return ok(toJson("error"));
    }


}
Sign up to request clarification or add additional context in comments.

1 Comment

btw. form() is deprecated
2

I'm pretty sure that author already found solution for those 2 years :), but today I was on same trouble and probably my nuance will help someone:

I used same methods to get POST parameters:

request().body().asFormUrlEncoded().get("from")[0];

And I got error too. But error was because of different POST type. So in my case I just was need to expect Multipart Form Data like in next variant:

request().body().asMultipartFormData().asFormUrlEncoded().get("from")[0];

So - just be a bit more careful with data that you are sending and data that you are expecting :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.