title: JSON post with asynchttpclient
body: <p>I'm trying to do a POST request on a REST service with async support (provided by atmosphere). Here's what my service looks like:</p>
  
  <pre><code>package co.damt.services;
  
  
  import javax.ws.rs.Consumes;
  import javax.ws.rs.Path;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Produces;
  
  import org.atmosphere.annotation.Broadcast;
  import org.atmosphere.annotation.Suspend;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  /**
   * REST Web Service
   *
   * @author damt
   */
  @Path("/")
  public class GenericResource {
  
      private final Logger logger = LoggerFactory.getLogger(GenericResource.class);
  
      @Suspend(contentType = "application/json")
      @GET
      public String suspend() 
      {
          return "";
      }
  
      @Broadcast(writeEntity = false)
      @POST
      @Produces("application/json")
      public Message broadcast(Message message) 
      {
          logger.info(message.getAuthor()+"@"+message.getTime()+": "+message.getMessage());
          return message;
      }
  
  }
  </code></pre>
  
  <p>I'm trying to do a POST request using <a href="https://github.com/AsyncHttpClient/async-http-client" rel="nofollow">asyncHttpClient</a> and I get a status of 200, but no log from the service that the message was received:</p>
  
  <pre><code>private final static ObjectMapper mapper = new ObjectMapper();
  private static Request request;
  
  public static void main( String[] args )
  {
  
      AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
  
      String jsonContent="";
      try {
          jsonContent = mapper.writeValueAsString(new Message("diego", "Hello World"));
          System.out.println(jsonContent);
      } catch (IOException ex) {
          Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
      }
  
      request = asyncHttpClient.preparePost("http://localhost:8080/Test/").
              setHeader("Content-Type","application/json").
              setHeader("Content-Length", ""+jsonContent.length()).
              setBody(jsonContent).
              build();
  
      ListenableFuture&lt;Integer&gt; f= null;
      try {
          f = asyncHttpClient.executeRequest(request,
                  new AsyncCompletionHandler&lt;Integer&gt;(){
  
                      @Override
                      public Integer onCompleted(Response response) throws Exception{
                          // Do something with the Response
                          System.out.println(response.getStatusCode());
                          return response.getStatusCode();
                      }
  
                      @Override
                      public void onThrowable(Throwable t){
                          // Something wrong happened.
                      }
                  });
  
      } catch (IOException ex) {
          Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
      }
  
      int statusCode;
  
      if (f!=null)
      {
          try {
              statusCode = f.get();
          } catch (InterruptedException ex) {
              Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
          } catch (ExecutionException ex) {
              Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      asyncHttpClient.close();
  </code></pre>
  
  <p>When I use <a href="http://hc.apache.org/httpclient-3.x/" rel="nofollow">Apache HttpClient</a> the service logs that the message was received and get a 200 status code:</p>
  
  <pre><code>    @SuppressWarnings("deprecation")
      final HttpClient httpClient = new DefaultHttpClient();
  
      @SuppressWarnings("deprecation")
      StringEntity requestEntity= null;
      try {
          requestEntity = new StringEntity(
              mapper.writeValueAsString(new Message("diego", "Hello World!")),
              ContentType.APPLICATION_JSON);
      } catch (UnsupportedEncodingException ex) {
          Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
      } catch(IOException ex) {
          Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
      }
  
      HttpPost postMethod = new HttpPost("http://localhost:8080/Test/chat/");
      postMethod.setEntity(requestEntity);
  
      HttpResponse statusCode= null;
      try {
          statusCode = httpClient.execute(postMethod);
      } catch (IOException ex) {
          Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
      }
  
      postMethod.releaseConnection();
  
      if (statusCode != null)
      {
          System.out.println(statusCode.toString());
      }
  </code></pre>
  
  <p>Why is asyncHttpClient not working?</p>