title: micronaut HttpResponse reading body text
body: <p>Trying to capture the response.body() text which is actually xml.</p>
  
  <pre><code>@Get(uri="/testxml")
  public String testxml() {
      System.out.println("Reading Response body test");
      HttpResponse response = streamClient.test();
      CompositeByteBuf content = (CompositeByteBuf) response.body();
  
      byte[] bytes = new byte[content.readableBytes()];
      int readerIndex = content.readerIndex();
      content.getBytes(readerIndex, bytes);
      String read = new String(bytes).trim();
      //System.out.println("RES"+ read);
      return read;
  
  }
  </code></pre>
  
  <p>I have the above code in this <a href="https://github.com/vahidhedayati/micronaut-java-groovy-comparison/blob/master/test-java/src/main/java/comparison/java/groovy/controller/OrdersController.java#L63-L76" rel="nofollow noreferrer">java micronaut application</a> and again <a href="https://github.com/vahidhedayati/micronaut-java-groovy-comparison/blob/master/test-groovy/src/main/groovy/comparison.java.groovy/controller/OrdersController.groovy#L39-L52" rel="nofollow noreferrer">in this groovy application</a>. and it appears when I hit the Java application <a href="http://localhost:8081/orders/testxml" rel="nofollow noreferrer">http://localhost:8081/orders/testxml</a>  for the very first time I see:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;current_orders&gt;&lt;order&gt;&lt;name&gt;a1&lt;/name&gt;&lt;description&gt;a1&lt;/description&gt;&lt;price&gt;12.2200000000000006394884621840901672840118408203125&lt;/price&gt;&lt;/order&gt;&lt;/current_orders&gt;
  </code></pre>
  
  <p>but any additional attempts on the java results in </p>
  
  <pre><code>{"message":"Internal Server Error: refCnt: 0"}
  </code></pre>
  
  <p>and trace shows:</p>
  
  <pre><code>Reading Response body test
  09:32:04.799 [nioEventLoopGroup-1-8] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: refCnt: 0
  io.netty.util.IllegalReferenceCountException: refCnt: 0
          at io.netty.buffer.AbstractByteBuf.ensureAccessible(AbstractByteBuf.java:1446)
          at io.netty.buffer.AbstractByteBuf.checkIndex(AbstractByteBuf.java:1376)
          at io.netty.buffer.AbstractByteBuf.checkDstIndex(AbstractByteBuf.java:1401)
          at io.netty.buffer.CompositeByteBuf.getBytes(CompositeByteBuf.java:854)
          at io.netty.buffer.CompositeByteBuf.getBytes(CompositeByteBuf.java:44)
          at io.netty.buffer.AbstractByteBuf.getBytes(AbstractByteBuf.java:487)
          at io.netty.buffer.CompositeByteBuf.getBytes(CompositeByteBuf.java:1744)
          at comparison.java.groovy.controller.OrdersController.testxml(OrdersController.java:71)
          at comparison.java.groovy.controller.$OrdersControllerDefinition$$exec4.invokeInternal(Unknown Source)
          at io.micronaut.context.AbstractExecutableMethod.invoke(AbstractExecutableMethod.java:145)
          at io.micronaut.context.DefaultBeanContext$BeanExecutionHandle.invoke(DefaultBeanContext.java:2401)
          at io.micronaut.web.router.AbstractRouteMatch.execute(AbstractRouteMatch.java:237)
          at io.micronaut.web.router.RouteMatch.execute(RouteMatch.java:123)
          at io.micronaut.http.server.netty.RoutingInBoundHandler.lambda$buildResultEmitter$13(RoutingInBoundHandler.java:1255)
          at io.reactivex.internal.operators.flowable.FlowableCreate.subscribeActual(FlowableCreate.java:71)
          at io.reactivex.Flowable.subscribe(Flowable.java:14479)
          at io.reactivex.internal.operators.flowable.FlowableMap.subscribeActual(FlowableMap.java:37)
          at io.reactivex.Flowable.subscribe(Flowable.java:14479)
          at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
          at io.reactivex.Flowable.subscribe(Flowable.java:14479)
          at io.reactivex.Flowable.subscribe(Flowable.java:14429)
          at io.micronaut.http.context.ServerRequestTracingPublisher.lambda$subscribe$0(ServerRequestTracingPublisher.java:53)
          at io.micronaut.http.context.ServerRequestContext.with(ServerRequestContext.java:53)
          at io.micronaut.http.context.ServerRequestTracingPublisher.subscribe(ServerRequestTracingPublisher.java:53)
          at io.reactivex.internal.operators.flowable.FlowableFromPublisher.subscribeActual(FlowableFromPublisher.java:29)
          at io.reactivex.Flowable.subscribe(Flowable.java:14479)
          at io.reactivex.Flowable.subscribe(Flowable.java:14426)
          at io.reactivex.internal.operators.flowable.FlowableSubscribeOn$SubscribeOnSubscriber.run(FlowableSubscribeOn.java:82)
          at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker$BooleanRunnable.run(ExecutorScheduler.java:260)
          at io.reactivex.internal.schedulers.ExecutorScheduler$ExecutorWorker.run(ExecutorScheduler.java:225)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
          at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>The Groovy application doesn't appear to ever actually load anything up and upon further inspection I noticed that in Java the raw representation of:</p>
  
  <pre><code>byte[] bytes = new byte[content.readableBytes()];
  System.out.println("Bytes = "+bytes); 
  </code></pre>
  
  <p>Comes out something like <code>[h87aa..</code> where as groovy version shows <code>[00 11 00 ]</code> a long array of numbers. </p>
  
  <p>Ultimately I am trying to send xml from 1 app and parse it on the other and perhaps there is a much much easier way. Currently stuck as to why this odd behaviour is happening </p>