title: HTTP 400 Bad Request - Invalid Header with Retrofit on Android
body: <p>I'm migrating my Android project from <a href="http://hc.apache.org/httpcomponents-client-4.3.x/index.html" rel="nofollow">Apache HttpClient</a> to <a href="http://square.github.io/retrofit/" rel="nofollow">Retrofit</a> with <a href="http://square.github.io/okhttp/" rel="nofollow">OkHttp</a>. POST/PUT request return error, even though the application custom headers are the same and Content-Length of both request are equal.</p>
  
  <ul>
  <li>GET: work fine.</li>
  <li>POST/PUT: <code>Http Error 400 Bad Request - Invalid Header</code>. even though </li>
  </ul>
  
  <p>For reference, the server is implemented using <code>ASP.NET</code> on <code>Windows Azure</code>.</p>
  
  <p><strong>Apache HttpClient</strong>:</p>
  
  <pre><code>HttpPut put = new HttpPut(&lt;URL&gt;);
  StringEntity se = new StringEntity(json);
  se.setContentEncoding("UTF-8");
  se.setContentType("application/json");
  put.setEntity(se);
  response = client.execute(put);
  </code></pre>
  
  <p><strong>Retrofit</strong> + <strong>OkHttp</strong>:</p>
  
  <pre><code>// API
  @PUT("/&lt;path&gt;")
  Response updateThing(@Body Dto dto);
  
  // OkHttpClient
  @Override
  public Response execute(Request r) throws IOException {
      List&lt;Header&gt; headers = HttpHelper.headerFor(method, url, etc);
  
      // Create new request with required headers
      Request encodedRequest = new Request(method, url, headers, r.getBody());
      return super.execute(encodedRequest);
  }
  </code></pre>
  
  <p>The server returns:</p>
  
  <pre><code>---&gt; HTTP PUT https://example.com/path
  Content-Type: application/json; charset=UTF-8
  Content-Length: 53
  {"user_id":"0","latitude":"0.0","longitude":"0.0"}
  ---&gt; END HTTP (53-byte body)
  PUT https://example.com/path
  Authorization Basic bmfweEskWkvlabF0ZSQ=
  Content-Type application/json
  Accept application/json
  HashKey 8eb93b898ddc2b74685ed2be64c76cc3af973ebc6b628781fe50eedcbe29376f
  &lt;--- HTTP 400 https://example.com/path (1397ms)
  : HTTP/1.1 400 Bad Request
  Connection: close
  Content-Length: 339
  Content-Type: text/html; charset=us-ascii
  Date: Sat, 04 Jan 2014 08:26:45 GMT
  OkHttp-Received-Millis: 1388824004870
  OkHttp-Response-Source: NETWORK 400
  OkHttp-Selected-Transport: http/1.1
  OkHttp-Sent-Millis: 1388824004785
  Server: Microsoft-HTTPAPI/2.0
  
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"&gt;
  &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Bad Request&lt;/TITLE&gt;
  &lt;META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"&gt;&lt;/HEAD&gt;
  &lt;BODY&gt;&lt;h2&gt;Bad Request - Invalid Header&lt;/h2&gt;
  &lt;hr&gt;&lt;p&gt;HTTP Error 400. The request has an invalid header name.&lt;/p&gt;
  &lt;/BODY&gt;&lt;/HTML&gt;
  
  &lt;--- END HTTP (339-byte body)
  </code></pre>
  
  <p><strong>Edit</strong>:</p>
  
  <ul>
  <li>Sending POST/PUT requests using online <a href="http://www.hurl.it/" rel="nofollow">Hurl.it</a> works.</li>
  <li>Sending POST/PUT requests without any body using Retrofit works too.</li>
  </ul>