Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")?

like image 944
Jon Avatar asked Oct 18 '25 05:10

Jon


1 Answers

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

like image 114
Jesse Wilson Avatar answered Oct 20 '25 18:10

Jesse Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!