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

Always return response content length is -1 #3309

Closed
serkandaglioglu opened this issue Apr 25, 2017 · 6 comments
Closed

Always return response content length is -1 #3309

serkandaglioglu opened this issue Apr 25, 2017 · 6 comments

Comments

@serkandaglioglu
Copy link

serkandaglioglu commented Apr 25, 2017

I tried with a lot of different urls but always response content length is -1

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();

Request request = new Request.Builder()
                .addHeader("Referer", "http://www.google.com")
                .addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:52.0) Gecko/20100101 Firefox/52.0")
                .addHeader("Connection", "keep-alive");
                .url(url).build();
Response response = client.newCall(request).execute();

boolean isSuccessful = response.isSuccessful();
int contentLength = response.body().contentLength();

Response return 200 code but contentLength is equal -1 and isSuccessful is true. I dont know what should I do? I tried 20 different urls but result is same.

NOTES:

  • I use emulator.
  • gradles >> classpath 'com.android.tools.build:gradle:2.3.1' and compile 'com.squareup.okhttp3:okhttp:3.7.0'
@JakeWharton
Copy link
Member

A content length of -1 means unknown. You can still call .string() or .bytes() or stream the body with .source(). This is usually because of transparent gzip which prevents us from knowing the actual body length.

@serkandaglioglu
Copy link
Author

@JakeWharton you are right. I debugged responseBody.string() is success. So it means there is no problem with my code?

@JakeWharton
Copy link
Member

Correct. It just means that the length isn't known and must be streamed.

@serkandaglioglu
Copy link
Author

@JakeWharton thanks a lot. you saved my time

@sureshmaidaragi1
Copy link

sureshmaidaragi1 commented Jan 25, 2023

A content length of -1 means unknown. You can still call .string() or .bytes() or stream the body with .source(). This is usually because of transparent gzip which prevents us from knowing the actual body length.

@JakeWharton response.body?.bytes()?.size or response.body?.string()?.length will it return the actually response received in bytes?

@sureshmaidaragi1
Copy link

Also regarding request.body?.contentLength() returning -1 why is it so?

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

No branches or pull requests

3 participants