POST-请求Vert.x和JavaScript有什么区别?
What is difference between POST-request of Vert.x and JavaScript?
我有 vert.x 申请。在我的verticle中,我有这样的路线来执行post-request:
router.post("/api/1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response
.putHeader("content-type", "text/html")
.end("Response from api");
});
好的,我想测试这个请求。
为此我创建了单元测试:
@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();
}
但是当我尝试执行这个测试时,我总是得到 404 错误。为了定义这个原因,我使用了 Postman(REST 客户端)。它使用以下请求:
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
而且这个请求也是return 404。
很奇怪。
所以我决定创建简单的 post-来自 JavaScript 的请求 - 我使用了非常简单的代码形式 JQuery:
$.post("/api/1");
它 return 是我期望的正确字符串。
谁能给我解释一下这三个问题的区别
在你的
HttpClientRequest request = client.post(8080, "localhost", "api/1", response -> {
...
开头漏了一个“/”,应该是:
HttpClientRequest request = client.post(8080, "localhost", "/api/1", response -> {
...
我有 vert.x 申请。在我的verticle中,我有这样的路线来执行post-request:
router.post("/api/1").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response
.putHeader("content-type", "text/html")
.end("Response from api");
});
好的,我想测试这个请求。
为此我创建了单元测试:
@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();
}
但是当我尝试执行这个测试时,我总是得到 404 错误。为了定义这个原因,我使用了 Postman(REST 客户端)。它使用以下请求:
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
而且这个请求也是return 404。
很奇怪。
所以我决定创建简单的 post-来自 JavaScript 的请求 - 我使用了非常简单的代码形式 JQuery:
$.post("/api/1");
它 return 是我期望的正确字符串。
谁能给我解释一下这三个问题的区别
在你的
HttpClientRequest request = client.post(8080, "localhost", "api/1", response -> {
...
开头漏了一个“/”,应该是:
HttpClientRequest request = client.post(8080, "localhost", "/api/1", response -> {
...