Http Post for WebClient



我正在尝试使用Vertx的WebClient测试我的Post端点,并且总是得到500作为状态代码。有人能告诉我我在这里做错了什么吗:-

final String jsonBody = "{"url": "https://www.google.se"}";
WebClient.create(vertx)
.post(8080, "::1", "/service")
.sendJson(
jsonBody,
response ->
testContext.verify(
() -> {
System.out.println(response.result().statusCode());
assertEquals("OK", response.result());
}));

500是一个内部服务器错误。在您的案例中,它没有获得所需的数据。。我想。所以尝试使用发送有效载荷

  • 将字符串转换为jsonobject并发送using sendJsonObject方法
  • 将缓冲区转换为jsonobject并发送using sendBuffer方法

这应该对您有效。我显示了Vert.x客户端和处理程序。客户端根据字符串创建一个JsonObject。处理程序在服务器中。

@Test
public void testPostURL(TestContext context) {
Async async = context.async();
final String body = "{"url": "https://www.google.se"}";
WebClient.create(vertx)
.post(8080, "localhost", "/service")
.putHeader("content-type", "application/json")
.sendJson( new JsonObject(body),
requestResponse -> {
context.assertEquals(requestResponse.result().statusCode(), 200);
async.complete();
});
}

处理程序需要JsonObject并返回url(https://www.google.se)

private void service(RoutingContext rc) {
HttpServerResponse response = rc.response();
JsonObject body = rc.getBodyAsJson();
String site = body.getString("url");
response.setStatusCode(200)
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(site));
}

最新更新