Eugene Shmorgun Eugene Shmorgun - 2 months ago 23
Javascript Question

What is difference between POST-request of Vert.x and JavaScript?

I have vert.x application. In my verticle I have such route to perform post-request:

router.post("/api/1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response
.putHeader("content-type", "text/html")
.end("Response from api");
});


OK, I want to test this request.

To do this I created unit-test:

@Test
public void testApi1(TestContext context) {
Async async = context.async();
HttpClient client = vertx.createHttpClient();
HttpClientRequest request = client.post(8080, "localhost", "api/1", response -> {

System.out.println("Some callback " + response.statusCode());
async.complete();

});
String body = "{'username':'www','password':'www'}";
request.putHeader("content-length", "1000");
request.putHeader("content-type", "application/x-www-form-urlencoded");
request.write(body);
request.end();
}


But when I try to execute this test I always get 404-error. In order to define the reason of this I used Postman (REST-client). It uses the follow request:

POST /api/1 HTTP/1.1
Host: localhost:8080
Cache-Control: no-cache
Postman-Token: 6065383d-8f51-405c-08fd-9cc824a22f92
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW


And this request return 404 too.

Very strange.

So I decided to create simple post-request from JavaScript - I used extremely simple code form JQuery:

$.post("/api/1");


And it returns me correct string, which I expect.

Who can explain me difference between these three issues.

Answer

In your HttpClientRequest request = client.post(8080, "localhost", "api/1", response -> { ...

You missed a "/" in the beginning, it should be:

HttpClientRequest request = client.post(8080, "localhost", "/api/1", response -> { ...

Comments