0

I am building a RESTful API in Play and I am getting a strange error with my 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(from:String, subject:String, message:String)

# Map static resources from the /public folder to the /assets URL path
GET     /assets/*file               controllers.Assets.at(path="/public", file)

The /api/createMessage is giving me some problems, when I post a full payload to it I get the error:

For request 'POST /api/createMessage' [Missing parameter: from]

Controller:

public static Result createMessages(String from, String subject, String message){
    Message.create(from, subject, message);
    return ok(toJson("ok"));
}

Request body:

Request Url: http://localhost:9000/api/createMessage
Request Method: POST
Status Code: 400
Params: {
    "from": "[email protected]",
    "subject": "Hello",
    "message": "World"
}

Can anybody help me out?

1
  • Added the controller in, thanks! Commented May 19, 2014 at 16:07

2 Answers 2

1

Problem is that in order to pass parameters to Java methods through the routes files, these parameters should be part of your endpoint's URL, which should be something similar to POST /api/createMessage/:from/:subject/:message

This is not a good idea for your case, and what you are trying to do (passing the parameters from a json object) is actually the way to go. So what you should do is replace the line in routes file with:

POST    /api/createMessage          controllers.Application.createMessages()

and read the JSON object from withing the createMessages method:

JsonNode data = request().body().asJson();
String from = data.get("from").textValue();
String subject = data.get("subject").textValue();
String message = data.get("message").textValue();

Just make sure you pass the Content-Type: application/json header with your request, so that play understands the request body as a JSON object.

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

Comments

0

problem is that you are sending data by post in your request

try

GET    /api/createMessage          controllers.Application.createMessages(from:String, subject:String, message:String)

and

Request Method: GET

this problem is because your request adding the params in POST

or, if you want in POST

POST    /api/createMessage          controllers.Application.createMessages

controller

public static Result createMessages{
String form =  //get it
String subject =  //get it
String message =  //get it
//not aware of java syntax too much but get here from form
Message.create(from, subject, message);
return ok(toJson("ok"));
}

as you tag of scala, means you are familar with scala so in scala controller be

def createMessages = Action{
     implicit request =>
    val form = request.body.asFormUrlEncoded.get("form")(0)
    var subject = request.body.asFormUrlEncoded.get("subject")(0)
    var message = request.body.asFormUrlEncoded.get("message")(0)
    Message.create(from, subject, message)
    OK("ok")
}

2 Comments

I want it to be post, I don't wish for the route to be a GET
@JacobClark updated the answer hope it will help you

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.