Can't view POST data

688 views
Skip to first unread message

Susan Lin

unread,
May 27, 2015, 12:39:21 AM5/27/15
to ve...@googlegroups.com

I am sending an ajax request from a client such as:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
        $.ajax({
        url: "http://192.168.1.74:8888",

        type: "POST",
        data: ({username: 'Bobby'})

    });
})
</script>
</head>
<body>
</body>
</html>

My Http Server is written in Java utilizing vertx is like so:

public class Main extends AbstractVerticle {


  @Override
  public void start() throws Exception {


       vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
        @Override
        public void handle(HttpServerRequest request) {

          System.out.println(request.getParam("username"));    

        }
    }).listen(8888);

  }
}

Every time I run the client, the server writes to console so the request is sent, but the server says the value is null. What am I doing wrong? How do I read the POST parameter being sent from the client?

UPDATE:

I found the problem, but no solution. If I change the ajax to GET from POST then it will appear. How do I make it so it works for POST and not for GET? SO the opposite of what is occurring now?

Cheers

Clement Escoffier

unread,
May 27, 2015, 2:04:25 AM5/27/15
to ve...@googlegroups.com
Hello,

The `getParams` method you are using retrieves query parameters. That’s why when using a GET request it works (because you JavaScript code passes parameters in the query when using GET).

To retrieve data from a form, you need to use the `getFormAttribute` method. However, to be used, the body need to be completely read. So you code would be something like:

vertx.createHttpServer().requestHandler(request -> {
if (request.method() == HttpMethod.POST) {
// If the HTTP method is POST, we enable body consumption
request.setExpectMultipart(true);
request.endHandler(event -> {
// The body is completely read
System.out.println(request.getFormAttribute("username"));
request.response().setStatusCode(200).end();
});
} else {
// Just return OK for all other methods
request.response().setStatusCode(200).end();
}
}).listen(8888);
Some explanations:

* when getting a POST request, I enables the form handling by using `setExpectMultipart(true)`. I did it for POST, you have to decide on which methods / content-type you want to enable this (form data and form url encoded should be fine). 
* I register an `endHandler` on the request called when the request body has been read. In the handler you can read your form attribute.

Cheers,

Clement
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Clement Escoffier

unread,
May 27, 2015, 2:19:47 AM5/27/15
to ve...@googlegroups.com
Hi,

Just in case you need, here is the code enabling the `setExpectMultipart` by checking the Content-Type header:

private final static List<String> EXPECT_MULTIPART;

static {
EXPECT_MULTIPART = Collections.unmodifiableList(
Arrays.asList("application/x-www-form-urlencoded", "multipart/form-data"));

}

@Override
public void start() throws Exception {
    vertx.createHttpServer().requestHandler(request -> {
final String content = request.headers().get("Content-Type");
if (EXPECT_MULTIPART.contains(content)) {

// If the HTTP method is POST, we enable body consumption
request.setExpectMultipart(true);
request.endHandler(event -> {
// The body is completely read
System.out.println(request.getFormAttribute("username"));
request.response().setStatusCode(200).end();
});
} else {
// Just return OK for all other methods
request.response().setStatusCode(200).end();
}
}).listen(8888);
}

Cheers,
Clement

Tim Fox

unread,
May 27, 2015, 3:10:16 AM5/27/15
to ve...@googlegroups.com
If you must use Vert.x core then do as Clement says below, but I'd recommend using vertx-web for this-  it's much simpler and you get the form attributes as params as you'd expect

Susan Lin

unread,
May 27, 2015, 10:21:42 PM5/27/15
to ve...@googlegroups.com
Is there any performance benefits to using either?

Thanks

Susan Lin

unread,
May 27, 2015, 10:46:58 PM5/27/15
to ve...@googlegroups.com
I tried the code using vertx-web:

routingContext.request().getParam("username"); 

but this returns null

Susan Lin

unread,
May 27, 2015, 11:15:21 PM5/27/15
to ve...@googlegroups.com
I now tried:

routingContext.request().endHandler(event -> {
System.out.println(routingContext.request().getFormAttribute("username"));
});

//Some other Code


This gets the param value, but it prints it after the //Some other code. I need it to print before in the same order the code is shown above.

Tim Fox

unread,
May 28, 2015, 2:41:46 AM5/28/15
to ve...@googlegroups.com
Performance difference should be minimal

Tim Fox

unread,
May 28, 2015, 2:42:15 AM5/28/15
to ve...@googlegroups.com
Please take a look at the docs and the tests for examples of how to use it properly.

Susan Lin

unread,
May 28, 2015, 10:26:42 PM5/28/15
to ve...@googlegroups.com
I had a look at http://vert-x3.github.io/docs/vertx-web/java/ but can't find how to retrieve POST data. The closest is "Capturing path params", but I do not want to store the variables in the parameter.

Tim Fox

unread,
May 29, 2015, 4:20:01 AM5/29/15
to ve...@googlegroups.com
Message has been deleted

Jez P

unread,
Jun 1, 2015, 4:28:46 AM6/1/15
to ve...@googlegroups.com
You still need a BodyHandler for the route - until then the form attributes won't be extracted 

On Monday, June 1, 2015 at 5:24:49 AM UTC+1, Victor Trot wrote:
This is my code now, but I still can't quite get it to work. The values returned are null:

HttpServer server = vertx.createHttpServer();

Router router = Router.router(vertx);

Route route = router.route(HttpMethod.POST, "/test/");


route.handler(routingContext -> {
      
try{    

//Without this an error is raised    
routingContext.request().setExpectMultipart(true);

//First method trying to get parameters
MultiMap attrs = routingContext.request().formAttributes(); 

//Sencond method to try to get parameters 
MultiMap params = routingContext.request().params();
  
  System.out.println(attrs.get("username")); //returns null
  System.out.println(params.get("username")); //returns null

}
catch(Exception e){
    System.out.println(e.getStackTrace());
}
  
});

server.requestHandler(router::accept).listen(8080);


  }

}

Susan Lin

unread,
Jun 2, 2015, 3:41:21 PM6/2/15
to ve...@googlegroups.com
Victor, did the bodyhandler work for you? Can you please share your code so that I can reference it?

Tim Fox

unread,
Jun 3, 2015, 5:12:32 AM6/3/15
to ve...@googlegroups.com
Please take a look at the test suite, there are examples that do exactly what you want.
--
Reply all
Reply to author
Forward
0 new messages