Jon
Jon

Reputation: 8021

how to add appropriate content-length header with okhttp?

I'm using okhttp to send requests to my server. The problem is that the requests okhttp sends are missing the content-length header - I can set the header manually with:

Request.builder
....
.addHeader("content-length","some-value")

but I can't get the appropriate value to put there. If I try to measure the body of my request like this:

int request_length = body.length();
builder.addHeader("content-length", String.valueOf(request_length)); 

then okhttp doesn't add the header at all.

Is there a correct way of adding the content-length header to requests (preferably a way of adding the header "by default")?

Upvotes: 4

Views: 3360

Answers (1)

Jesse Wilson
Jesse Wilson

Reputation: 40593

It's automatic if your ResponseBody returns a content length other than -1.

Upvotes: 6

Related Questions