HELP - I need help to send POST request to a Http Server from another Http Server

632 views
Skip to first unread message

Carlos Verde

unread,
Jan 17, 2018, 4:05:30 PM1/17/18
to vert.x
I try to use a webclient, like this.... 


@Override
    public void start(Future<Void> startFuture) throws Exception {
       
super.start(startFuture);
       
httpServer = vertx.createHttpServer();

       
Router router = Router.router(vertx);

        router
.route().handler(CookieHandler.create());

        router
.route().handler(SessionHandler
               
.create(LocalSessionStore.create(vertx))
       
);

        router
.route().handler(BodyHandler.create());

        router
.post("/route").handler(this::HandleDoSomething);



       
System.out.println("Servidor activo con el puerto 9070");
       
httpServer.requestHandler(router::accept).listen(9070);
   
}

   
private void HandleDoSomething(RoutingContext routingContext){
       
try{
           
WebClient client = WebClient.create(vertx);

           
final String[] resp = {null};
           
String keySN = "";
           
String data = routingContext.getBodyAsString();

           
JsonObject jData = new JsonObject(AES.decrypt(data,"1234567887654321"));

                client
                   
.post(9080, "localhost", "/services")
                   
.sendJsonObject(jData, ar -> {
                       
if (ar.succeeded()) {
                           
System.out.println("enviado al servidor");
                           
HttpResponse<Buffer> response = ar.result();
                           
System.out.println("response: " + response.toString());
                           
try{
                               
resp[0] = AES.decrypt(response.toString(),"1234567887654321");
                           
} catch (Exception e){
                                e
.printStackTrace();
                           
}
                       
} else {
                            ar
.cause().printStackTrace();
                       
}
                   
});


           
System.out.println("Recibo de: " + routingContext.request().connection().remoteAddress().host());
           
System.out.println("recibo: "+ resp[0]);
            routingContext
.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
            routingContext
.response().end(""+ resp[0]);
       
} catch (Exception e){
            e
.printStackTrace();
       
}
   
}

but I have this response when I do a POST using PostMan 

Jan 17, 2018 4:18:45 PM io.vertx.ext.web.impl.RoutingContextImplBase
SEVERE: Unexpected exception in route
java.lang.NoClassDefFoundError: io/vertx/ext/web/codec/impl/BodyCodecImpl
at io.vertx.ext.web.client.impl.WebClientImpl.request(WebClientImpl.java:178)
at com.ubiipagos.Verticles.WebBrutoServiceVerticle.HandleDoSomething(WebBrutoServiceVerticle.java:83)
at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:217)
at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:78)
at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:118)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$BHandler.doEnd(BodyHandlerImpl.java:202)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl$BHandler.end(BodyHandlerImpl.java:180)
at io.vertx.ext.web.handler.impl.BodyHandlerImpl.lambda$handle$0(BodyHandlerImpl.java:67)
at io.vertx.core.http.impl.HttpServerRequestImpl.handleEnd(HttpServerRequestImpl.java:406)
at io.vertx.core.http.impl.ServerConnection.handleEnd(ServerConnection.java:297)
at io.vertx.core.http.impl.ServerConnection.processMessage(ServerConnection.java:441)
at io.vertx.core.http.impl.ServerConnection.handleMessage(ServerConnection.java:131)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:673)
at io.vertx.core.http.impl.HttpServerImpl$ServerHandler.doMessageReceived(HttpServerImpl.java:580)
at io.vertx.core.http.impl.VertxHttpHandler.lambda$channelRead$0(VertxHttpHandler.java:71)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:335)
at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:193)
at io.vertx.core.http.impl.VertxHttpHandler.channelRead(VertxHttpHandler.java:71)
at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:122)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:373)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:351)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:373)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:351)
at io.vertx.core.http.impl.Http1xOrH2CHandler.end(Http1xOrH2CHandler.java:49)
at io.vertx.core.http.impl.Http1xOrH2CHandler.channelRead(Http1xOrH2CHandler.java:27)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:373)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:351)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:373)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:359)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:129)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:651)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:574)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:488)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:450)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:873)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: io.vertx.ext.web.codec.impl.BodyCodecImpl
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 44 more



Thomas SEGISMONT

unread,
Jan 17, 2018, 4:29:04 PM1/17/18
to ve...@googlegroups.com
Have you checked that the vertx-web-common JAR is on your classpath?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/034737cd-491a-47cd-8f1f-ccb9e67468dc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Carlos Verde

unread,
Jan 18, 2018, 7:58:05 AM1/18/18
to vert.x
Thanks, I wass missing that, but now it threw me this error in the response

            io.vertx.ext.web.client.impl.HttpResponseImpl@38aa2010

In the other Web Server I response like this: 

            String data = routingContext.getBodyAsString();
            JsonObject password = routingContext.getBodyAsJson();
            password.put("ip",routingContext.request().connection().remoteAddress().host());
            routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
            String resp = "Web Server 2 is receiving you";
            Buffer respBuffer = null;
            System.out.println("Respuesta buffer: "+respBuffer.appendString(AES.encrypt(resp,"1234567887654321")));
            respBuffer.appendString(AES.encrypt(resp,"1234567887654321"));
            routingContext.response().end(respBuffer);
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Grey Seal

unread,
Jan 18, 2018, 8:06:13 AM1/18/18
to vert.x
Not sure about the error but to send back the response of the POST call, move the lines below inside the try block.

 System.out.println("Recibo de: " + routingContext.request().connection().remoteAddress().host());
System.out.println("recibo: "+ resp[0]);
routingContext
.response().putHeader(HttpHeaders.CONTENT_TYPE, "text/plain");
routingContext
.response().end(""+ resp[0]);


Carlos Verde

unread,
Jan 18, 2018, 8:12:10 AM1/18/18
to vert.x
And I think the web server runs so fast and doesn't capture the  HttpResponse<Buffer> response = ar.result();  because when I show it, It is ever null

El miércoles, 17 de enero de 2018, 17:29:04 (UTC-4), Thomas Segismont escribió:
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Jez P

unread,
Jan 18, 2018, 8:19:39 AM1/18/18
to vert.x
Grey Seal's post tells you what you need to do. 

You're ending the response immediately while you're still waiting for the callback to happen. You need to end your response in the callback.

Grey Seal

unread,
Jan 18, 2018, 8:39:16 AM1/18/18
to vert.x
It depends on the response from the POST call. You can verify the same using System.out.println(response.bodyAsXXX). 

Jez P

unread,
Jan 18, 2018, 9:11:11 AM1/18/18
to vert.x
Also this isn't an error: io.vertx.ext.web.client.impl.HttpResponseImpl@38aa2010 

You have the following in your code:

System.out.println("response: " + response.toString());

The default toString implementation, assuming reponse is an HttpResponseImpl would be similar to that output.

Carlos Verde

unread,
Jan 19, 2018, 8:05:12 AM1/19/18
to vert.x
Thanks, was that, I changed the thing I respond and I use getBody....() to get the response
Reply all
Reply to author
Forward
0 new messages