Search code examples
reactorreactor-netty

Why can't subscribe the request in reactor-netty?


I just want access the Http content in reactor-netty project. But the result is null.

Code is below.

DisposableServer server =
            HttpServer.create()
                    .host("localhost")
                    .port(8000)
                    .route(routes ->
                                    .post("/echo",
                                            (request, response) -> 
                                                {   request.receive()
                                                        .retain()
                                                        .aggregate()
                                                        .asString()
                                                        .subscribe(System.out::println);
                                                    return response.sendString(Mono.just("hello"));})          
                    .bindNow();

I can't get the rerult in the console.

Could I access the request as what I do in the code? Anyone can help? Thanks.


Solution

  • You return the response before the request data is received, so Reactor Netty will drop any incoming data that is received AFTER the response is sent.

    I don't know your use case but changing the example to this below, you will be able to see the incoming data:

    DisposableServer server =
            HttpServer.create()
                    .host("localhost")
                    .port(8000)
                    .route(routes ->
                            routes.post("/echo",
                                    (request, response) ->
                                            response.sendString(request.receive()
                                                    .retain()
                                                    .aggregate()
                                                    .asString()
                                                    .flatMap(s -> {
                                                        System.out.println(s);
                                                        return Mono.just("hello");
                                                    })))
                    )
                    .bindNow();