user2924127
user2924127

Reputation: 6240

Http Server (Java on Vertx) not getting POST parameter

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

Upvotes: 3

Views: 4207

Answers (3)

petrenkotino
petrenkotino

Reputation: 31

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());
  });
});

Upvotes: 3

Izhar Lotem
Izhar Lotem

Reputation: 407

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.

Upvotes: 3

Alaa Abuzaghleh
Alaa Abuzaghleh

Reputation: 1009

data: {"username": 'Bobby'} will fix your issue, and remove the () also you can try to change you ajax request in jquery as follow

var datavar = {username:"someusername"}; //Array 

$.ajax({
    url : "AJAX_POST_URL",
    type: "POST",
    data : datavar,
    success: function(data, textStatus, jqXHR)
    {
       alert("success") ; 
    },
    error: function (jqXHR, textStatus, errorThrown)
    {
 alert("fail") ; 
    }
});

Upvotes: 2

Related Questions