Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

org.apache.http.ProtocolException: Content-Length header already present #454

Closed
rnevet opened this issue Mar 31, 2014 · 11 comments · Fixed by #511
Closed

org.apache.http.ProtocolException: Content-Length header already present #454

rnevet opened this issue Mar 31, 2014 · 11 comments · Fixed by #511

Comments

@rnevet
Copy link

rnevet commented Mar 31, 2014

I think that Version 1.5 breaks MultipartTypedOutput:

I'm trying to send a File or ByteArray to the server and getting the "Content-Length header already present" error.

As far as I can see the relevant change was added in RequestBuilder adding Content-Length header.
https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/RequestBuilder.java
from line 326:

      long length = body.length();
      if (length != -1) {
        addHeader("Content-Length", String.valueOf(length));
      }
@w4lk3r
Copy link

w4lk3r commented Mar 31, 2014

I met the exception too.
Maybe shouldn't supply the "Content-Length" header when chose "HttpClient" Connector.
Use okHttp and UrlConnection would be OK.

@rnevet
Copy link
Author

rnevet commented Mar 31, 2014

I'm not supplying the Content-Length anywhere in my code, but because it's a Multipart message it's is added for each part of the message, and I think since it's added at this point also the exception is thrown. As a test I added to the if clause a check that prevents adding the "Content-Length" if it's a Multipart message and this seems to solve the issue.

@w4lk3r
Copy link

w4lk3r commented Mar 31, 2014

Sorry, I means the Content-Length header was added by retrofit (In POST request).
But Apache's HttpClient library would add it again, and would throw this exception when checked the header was already present.
What can we do?

@rnevet
Copy link
Author

rnevet commented Mar 31, 2014

I would like to get an opinion from someone more experienced with Retrofit and correct HTTP requests.
I solved it on a local snapshot by checking that the message isn't multipart like this:
https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/RequestBuilder.java#L327

      long length = body.length();
      if (length != -1 && multipartBody == null) {
        addHeader("Content-Length", String.valueOf(length));
      }

But I don't know if this is a valid solution for all cases and additionally I needed to fix some test cases that are expecting to find "Content-Length".

@SamsonAkisanya
Copy link

I'm having the same problem too connecting to a https service..Not sure what to do now

@nicogiagnoni
Copy link

Same problem here, I'm using last version of RoboSpice-Retrofit module (It uses Retrofit 1.5), and my App is crashing in all POSTs (JSON too). We have implemented our own ApacheClient, so we are getting this exception: http://docjar.org/docs/api/org/apache/http/protocol/RequestContent.html:

if (request.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}

We don't use OkHttp because of SSL Exception, it crashes when it's combined with GAnalytics lib, and we don't use HttpURLConnection because of GZIP + Cache decoding bug.

Our solution was stay at Retrofit 1.4.1 version, it's working OK.

@SamsonAkisanya
Copy link

Found a way around it.. similar approach you used

On 23 April 2014 16:43, Nicolas Giagnoni notifications@github.com wrote:

Same problem here, I'm using last version of RoboSpice-Retrofit module (It
uses Retrofit 1.5), and my App is crashing in all POSTs (JSON too). We have
implemented our own ApacheClient, so we are getting this exception:
http://docjar.org/docs/api/org/apache/http/protocol/RequestContent.html:

if (request.containsHeader(HTTP.CONTENT_LEN)) {
throw new ProtocolException("Content-Length header already present");
}

We don't use OkHttp because of SSL Exception, it crashes when it's
combined with GAnalytics lib, and we don't use HttpURLConnection because of
GZIP + Cache decoding bug.

Our solution was stay at Retrofit 1.4.1 version, it's working OK.


Reply to this email directly or view it on GitHubhttps://github.com//issues/454#issuecomment-41177191
.

Opinions expressed in this email are those of the writer and not
necessarily those of YUZA http://yuza.com/. E-mail communication is
monitored within YUZA and messages may be viewed. This e-mail and any files
with it are solely for the use of the addressee(s). If you are not the
intended recipient, you have received this e-mail in error. Please delete
it, return it to the sender or forward by email to admin@yuza.com. YUZA is
a registered trademark of Yuza Holdings Ltd.

@rnevet
Copy link
Author

rnevet commented Apr 24, 2014

It would be great if someone can review the change I suggested and we can create a fix for the next release.

@nealsanche
Copy link

I just hit this as well, on a POST to an SSL server.

@grantland
Copy link

A ghetto fix:

public class AndroidApacheClient extends retrofit.client.ApacheClient {

  public AndroidApacheClient() {
    super(AndroidHttpClient.newInstance("Retrofit"));
  }

  @Override
  public Response execute(Request request) throws IOException {
    List<retrofit.client.Header> headers = request.getHeaders();
    List<retrofit.client.Header> modified = new ArrayList<retrofit.client.Header>();
    for (int i = 0; i < headers.size(); i++) {
      retrofit.client.Header header = headers.get(i);
      if (!header.getName().equals("Content-Length")) {
        modified.add(header);
      }
    }
    return super.execute(new Request(request.getMethod(), request.getUrl(), modified, request.getBody()));
  }
}

@christopherperry
Copy link

OkHttp was locking up on thread waits, so I switched to ApacheClient and now this. Argh.

Tried the ghetto fix above, back to locked up threading issues. Is there a more reliable older release?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants