Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Http Server (Java on Vertx) not getting POST parameter

Tags:

java

http

vert.x

I am sending a 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

like image 280
user2924127 Avatar asked Aug 08 '26 17:08

user2924127


2 Answers

I encountered the problem while working on my project. I was using Dojo on the client side. I manged to solve this by making adjustments both on the client side and the server side.

Client:

var json = JSON.stringify({
    "username": "Bobby"
}); 
request.post("yoururl", {
    data: json,
    headers: {
       "Content-Type": "application/javascript"
    }
});

On the server side, apparently, what was required was calling the method BodyHandler.create() before handling the code.

router.route(HttpMethod.POST, "yoururl").handler(BodyHandler.create());
router.route(HttpMethod.POST, "yoururl").handler(routingContext -> 
{    
    String sectionType = routingContext.request().getParam("sectionId");
    JsonObject j = routingContext.getBodyAsJson();
});

I hope this would solved your problem.

like image 66
Izhar Lotem Avatar answered Aug 10 '26 07:08

Izhar Lotem


When doing a POST request the data is in the body. On the server side you need to register a body handler for your router in order to be able to easily get the body from the request. You can do that like this:

final Router router = Router.router(vertex);
// Adding a BodyHandler for routes
router.route().handler(BodyHandler.create());
router.post("/your/endpoint").handler(routingContext -> {
   System.out.println(routingContext.getBodyAsString());
});

Another option is to add another callback handler like this:

final Router router = Router.router(vertex);
router.post("/your/endpoint").handler(routingContext -> {
  routingContext.request().bodyHandler(body -> {
    System.out.println(body.toString());
  });
});
like image 23
petrenkotino Avatar answered Aug 10 '26 07:08

petrenkotino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!