Vert.x: How to send a post request?

905 Views Asked by At

My first Vertx Web app :

I expect To get the index.html at localhost.8080/Test then find a way to retrieve the data, but the page doesn't show

I have a RequestResponseExample class:

public class RequestResponseExample extends AbstractVerticle {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        Router router = Router.router(vertx);

        router.post("/Test").handler(rc -> rc.response().sendFile("index.html"));

        vertx.createHttpServer()
            .requestHandler(router)
            .listen(8080);
    }

}

And My Html Code index.html

<html>
<head>
  <meta charSet="UTF-8">
  <title>OTP Authenticator Verification Example Page</title>
</head>
<body>
<form action="/" method="post" encType="multipart/form-data">
  <div>
    <label>Code:</label>
    <input type="text" name="code"/><br/>
  </div>
  <div>
    <input type="submit" value="Submit"/>
  </div>
</form>
</body>
</html>

2

There are 2 best solutions below

4
Jonas Taulien On BEST ANSWER

Solution

Change router.post( to router.get(.

Description

Currently, you are configuring the Router to only handle HTTP POST request. That means, it is configured to respond to such an HTTP request:

POST /Test

But when you try to open localhost.8080/Test in your browser, it will send such a request to your server:

GET /Test

This is why you have to tell the router to handle GET and not POST requests.

Additional information: GET and POST are so called HTTP request methods. If you want to learn more about that, I recommend you to read the following article: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

About Verticles

In your code, you can remove extends AbstractVerticle and it will work the same way. If you want your code to get executed in the context of a verticle you have to create an instance of your class and then you have to deploy it:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

public class RequestResponseExample extends AbstractVerticle {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(new RequestResponseExample());
    }

    @Override
    public void start(){
        Router router = Router.router(vertx);

        router.get("/Test").handler(rc -> rc.response().sendFile("index.html"));
        router.post().handler(BodyHandler.create());
        router.post("/").handler(rc -> System.out.println(rc.request().formAttributes().get("code")));

        vertx.createHttpServer()
            .requestHandler(router)
            .listen(8080);
    }

}

Since I see a bit of confusion on your side, you may want to also read the following article about Verticles: https://vertx.io/docs/vertx-core/java/#_verticles

1
Ilyass Asri On

I think that you send a GET request, but you handle a POST request to return the html file. I think firstly you have to handle a GET request that's return the html page, and also write a route for the form.