Body Handler not working in subrouter

359 views
Skip to first unread message

Darrin West

unread,
Nov 3, 2015, 3:30:44 PM11/3/15
to vert.x
Using vertx 3.1.0. I know to add a BodyHandler before trying to access the body in a request. This works for paths with no path parameters:
router.route("/project").handler(BodyHandler.create()); // Enable body handling
router.post("/project").handler(routingContext -> {
// process the body

JsonObject body = routingContext.getBodyAsJson();
...

But this does doesn't seem to work when there are path paramters:
router.route("/storage").handler(BodyHandler.create()); // Enable body handling
router.post("/storage/p/:projectId/b").handler(routingContext -> {

// process the body
   JsonObject body = routingContext.getBodyAsJson(); // returns null
...

The POST handler runs, but getBodyAsJson() always returns null. routingContext.body is null.

I even tried this without effect:
router.route("/").handler(BodyHandler.create()); // Enable body handling

I will next try the "classic" method of a bodyHandler on the request:
request.bodyHandler(totalBuffer -> {
  System.out.println("Full body received, length = " + totalBuffer.length());
});

Darrin West

unread,
Nov 3, 2015, 3:33:05 PM11/3/15
to vert.x
Apologies for mentioning subrouter in the topic. That was an earlier suspect. It appears to be the path parameters causing the BodyHandler to not work.

Tim Fox

unread,
Nov 3, 2015, 3:33:55 PM11/3/15
to ve...@googlegroups.com
On 03/11/15 20:30, Darrin West wrote:
Using vertx 3.1.0. I know to add a BodyHandler before trying to access the body in a request. This works for paths with no path parameters:
router.route("/project").handler(BodyHandler.create()); // Enable body handling
router.post("/project").handler(routingContext -> {
            // process the body

            JsonObject body = routingContext.getBodyAsJson();
...

          
But this does doesn't seem to work when there are path paramters:
router.route("/storage").handler(BodyHandler.create()); // Enable body handling

Try "/storage*" or it won't match "/storage/p/whatever" too

router.post("/storage/p/:projectId/b").handler(routingContext -> {

   // process the body
   JsonObject body = routingContext.getBodyAsJson(); // returns null
...
The POST handler runs, but getBodyAsJson() always returns null. routingContext.body is null.
I even tried this without effect:
router.route("/").handler(BodyHandler.create()); // Enable body handling
I will next try the "classic" method of a bodyHandler on the request:
request.bodyHandler(totalBuffer -> {
  System.out.println("Full body received, length = " + totalBuffer.length());
});
--
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.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/9148127d-cea7-4e12-9f77-2dccb4329bdf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Darrin West

unread,
Nov 3, 2015, 4:03:38 PM11/3/15
to vert.x
Thanks Tim. The issue was needing a wildcard on the path parameters route. This works, and is able to access the body, even within a subrouter:

router.route("/storage*").handler(BodyHandler.create()); // Enable body handling
Reply all
Reply to author
Forward
0 new messages