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
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);
--
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.
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,ClementThis 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 raisedroutingContext.request().setExpectMultipart(true);//First method trying to get parametersMultiMap attrs = routingContext.request().formAttributes();//Sencond method to try to get parametersMultiMap params = routingContext.request().params();System.out.println(attrs.get("username")); //returns nullSystem.out.println(params.get("username")); //returns null}catch(Exception e){System.out.println(e.getStackTrace());}});server.requestHandler(router::accept).listen(8080);}}
--