Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8048061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:18:25+00:00 2026-06-05T06:18:25+00:00

I’ve got to say I’m a little confused about how to handle request parameter

  • 0

I’ve got to say I’m a little confused about how to handle request parameter using the new Play Framework 2. The data comes from different origins regarding how the request is made. So far, here’s the possibilities :

1 – If you do a simple GET :

ctx().request().queryString()

2 – If you do a POST using an HTML Form :

The form :

<form method="post" action="/">
    <input type="hidden" name="foo" value="bar" />
    <input type="hidden" name="t" value="1" />
    <input type="hidden" name="bool" value="true" />
    <input type="submit" name="submit" value="Submit" />
</form>

The method :

public static Result test() {
    ctx().request().queryString();             // {} ; As expected
    ctx().request().body();                    // contains data
    ctx().request().body().asFormUrlEncoded(); // contains data
    ctx().request().body().asJson();           // empty
    return ok();
}

This seems normal.

Now if I add @BodyParser.Of(BodyParser.Json.class) (suppose I accept both Ajax POST and normal POST for fallback in Non-JS case) :

@BodyParser.Of(BodyParser.Json.class)
public static Result test() {
    ctx().request().queryString();             // {} ; as Expected
    ctx().request().body();                    // contains data
    ctx().request().body().asFormUrlEncoded(); // empty : Shouldn't this contains data since I posted them via a simple form ?!
    ctx().request().body().asJson();           // empty
    return ok();
}

And then, the hell hapened : How can I get the values of a simple form if none of them are filled (asJson, asFormUrlEncoded, etc) ?!

3 – If you do a POST via AJAX :

// Code in JS used :
$.ajax({
    'url': '/',
    'dataType': 'json',
    'type': 'POST',
    'data': {'foo': 'bar', 't': 1, 'bool': true}
});

Result :

public static Result test() {
    ctx().request().queryString();             // {}
    ctx().request().body();                    // contains data
    ctx().request().body().asFormUrlEncoded(); // contains data
    ctx().request().body().asJson();           // empty
    return ok();
}

With the @BodyParser.Of(BodyParser.Json.class) :

@BodyParser.Of(BodyParser.Json.class)
public static Result test() {
    ctx().request().queryString();             // {}
    ctx().request().body();                    // contains data
    ctx().request().body().asFormUrlEncoded(); // empty
    ctx().request().body().asJson();           // empty : Shouldn't this contains data since I espect JSON ?!
    return ok();
}

Here the inconsistencies is the asJson() method that should return the data since, according to the doc

Note: This way, a 400 HTTP response will be automatically returned for non JSON requests. (http://www.playframework.org/documentation/2.0/JavaJsonRequests)

What I’d like to know is what is the best decorator+method for a POST that would accept a simple post from HTML or an Ajax Request with POST?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:18:27+00:00Added an answer on June 5, 2026 at 6:18 am

    I would recommend to use the Form classes provided by PlayFramework.
    The Form will bind its values to the provided request data.

    There are two different Form implementations:

    1. DynamicForm: POST data is available through Map accessors methods
    2. Form: the generic form of Form will map the POST data to an instance of an entity class [more information]

      The form also provides some usefull features like automatic type conversion, validation, error reporting and so on.

    a simple example with a dynamic form:

    ajax request:

    $.post("@routes.Application.movetodo()", 
        { "id": 123, "destination": destination }, function(data)
            {
               // do something when request was successfull
            });
    

    Routes file:

    GET     /                           controllers.Application.index()
    POST    /movetodo                   controllers.Application.movetodo()
    

    Controller implementation:

    public static Result movetodo()
    {
        DynamicForm data = form().bindFromRequest(); // will read each parameter from post and provide their values through map accessor methods
        // accessing a not defined parameter will result in null
        String id = data.get("id");
        String destination = data.get("destination");
    
        return ok();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.