title: Can't POST using loopj AsyncHttpClient and an authorization header
body: <p>I am using loopj (1.4.7) and AsyncHttpClient to post Json to a Web API server. My (simplified) code is:</p>
  
  <pre><code>private void createCode() {
      // 1. Get the current data and time
      DateFormat df = DateFormat.getDateTimeInstance();
      df.setTimeZone(TimeZone.getTimeZone("UTC"));
      String gmtTime = df.format(new Date());
  
      // 2. Setup few things
      AsyncHttpClient client = new AsyncHttpClient();
      Context context = this.getApplicationContext();
      connCode = "1234";
  
      JSONObject jsonData = new JSONObject();
      try {
          jsonData.put("ClientCode", connCode);
          jsonData.put("CodeCreated", gmtTime);
      } catch (Exception ex) {
          // Should not really happen
      }
  
      // 3. Authorization/Authentication
      String userName = "TestUser";
      String userPassword = "TestPassword";
      String userNamePassword = userName + ":" + userPassword;
      byte[] loginString = userNamePassword.getBytes();
      String loginStringEncoded = Base64.encodeToString(loginString, Base64.DEFAULT);
  
      client.setAuthenticationPreemptive(true);
  
      // This is the line that cause the problem
      client.addHeader("Authorization", loginStringEncoded);
  
      // 4. Setup various options
      StringEntity entity = null;
      try {
          entity = new StringEntity(jsonData.toString());
      } catch (UnsupportedEncodingException e) {
          return;
      }
      entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  
      // 5. Post the code to the database
      client.post(context, CODE_URL, entity, "application/json", new JsonHttpResponseHandler() {
  
          @Override
          public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
              // TODO - Process results...
          }
  
          @Override
          public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
          }
  
          @Override
          public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONArray errorResponse) {
          }
  
          @Override
          public void onFailure(int statusCode, Header[] headers, java.lang.Throwable throwable, org.json.JSONObject errorResponse) {
          }
      });
  }
  </code></pre>
  
  <p>The response is <code>“400 - Bad Request”</code> because not everything makes it to the server if I use the <code>"Authorization"</code> header. What shows up on the server is (verified using Fiddler):</p>
  
  <pre><code>{"ClientCode":"1234","CodeCreated":"Jul 17, 2015 1:03:00 AM
  </code></pre>
  
  <p>If I get rid of the <code>"client.addHeader("Authorization", loginStringEncoded);"</code> line, everything works fine and the server receives everything (but I need the Authorization header to limit access):</p>
  
  <pre><code>{"ClientCode":"1234","CodeCreated":"Jul 17, 2015 1:10:00 AM"}
  </code></pre>
  
  <p>I can tell that the server is processing the authorization properly but the response fails because of the missing <strong>"}"</strong>. I have verified that the whole string is part of the “entity” variable before the “client.post” command gets executed.</p>
  
  <p>If it makes a difference, I am running Android Studio on a Mac and I have verified using <code>WireShark</code> that the missing <strong>"}"</strong> is already not there when the Mac connects to the <code>Web API server</code>.
  What could be causing it? A similar setup works fine for <code>GET</code> but I cannot make it work for <code>POST</code>. Is there perhaps a better way to do this?
  Thanks.</p>