简体   繁体   English

Vert.x和JavaScript的POST请求之间有什么区别?

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

I have vert.x application. 我有vert.x应用程序。 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. 但是,当我尝试执行此测试时,我总是会收到404错误。 In order to define the reason of this I used Postman (REST-client). 为了定义此原因,我使用了邮递员(REST客户端)。 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. 并且此请求也返回404。

Very strange. 很奇怪。

So I decided to create simple post-request from JavaScript - I used extremely simple code form JQuery: 因此,我决定从JavaScript创建简单的后请求-我使用了非常简单的JQuery代码形式:

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

And it returns me correct string, which I expect. 它返回我正确的字符串,这是我期望的。

Who can explain me difference between these three issues. 谁能解释我这三个问题之间的区别。

In your HttpClientRequest request = client.post(8080, "localhost", "api/1", response -> { ... 在您的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 -> { ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM