我有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-error。为了定义原因,我使用了Postman (REST-client)。它使用以下请求:
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并且这个请求也返回404。
非常奇怪。
因此,我决定从JavaScript创建简单的post请求-我使用了极其简单的代码格式JQuery:
$.post("/api/1");它会返回正确的字符串,这是我所期望的。
谁能给我解释一下这三个问题的区别。
发布于 2016-09-17 18:21:11
在你的HttpClientRequest request = client.post(8080, "localhost", "api/1", response -> { ...中
你在开头漏掉了一个"/“,它应该是:
HttpClientRequest request = client.post(8080, "localhost", "/api/1", response -> { ...
https://stackoverflow.com/questions/39545330
复制相似问题