Cannot add Content-Length header to a multipart part
Maintainers usually reply within 1 day
@alexzargarian23 is already working on this.
Since Oct 24, 2026.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- kotlin
- Domain
- backend-api-design
Research direction
Start with MultipartBody.Part.create and the Content-Length require check quoted in the issue, then inspect nearby multipart tests. Confirm how manually supplied part headers are handled for multipart types such as multipart/mixed. Done means the factory accepts the header where permitted, with tests covering the behavior.
Written by the indexing model from the issue text.
Description
In #7319 the Content-Length header ceased being auto-included in every multipart part. That is fine, but manually adding that header to a part throws an exception:
MultipartBody.Part.create(
headers = Headers.Builder().add("Content-Length", TEST_DATA.size.toString()).build(),
body = TEST_DATA.toRequestBody(),
) // crash - "Unexpected header: Content-Length"
The crash is due to this line in the MultipartBody.Part.create method:
require(headers?.get("Content-Length") == null) { "Unexpected header: Content-Length" }
I think that require was added because that header was being auto-included at the time. Since that is no longer the case but the require still exists, it's impossible to add a Content-Length header using the factory method.
I believe including Content-Length on a multipart part is out-of-spec if the request is of type multipart/form-data, but is within RFC specs for other types like multipart/mixed.
Tests showing it's not possible to manually include Content-Length: https://gist.github.com/jasonrobinson-gpsw/830a56b81be94a704fad9409d8326865
- Dominant language
- Kotlin
- Stars
- 47.1k
- Forks
- 9.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 31
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from lysine-dev/okhttp
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
lysine-dev/okhttp#9761 · 3 comments ·
Maintainers usually reply within 1 day
-
build-ci
lysine-dev/okhttp#9815 · 1 comment ·
Maintainers usually reply within 1 day
-
Consider `ForwardingRequestBody`Possibly taken @Amir-Golmoradi claimed this 3 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 57/100
lysine-dev/okhttp#9800 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
lysine-dev/okhttp#9799 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 67/100
lysine-dev/okhttp#9796 ·
Maintainers usually reply within 1 day
All issues in lysine-dev/okhttp
Similar issues
-
Messages: Subscribe to the new communication websocket topicsPossibly taken @krusche claimed this today. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ls1intum/artemis-android#801 ·
Maintainers usually reply within 1 day
-
Bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
keiyoushi/extensions-source#20362 ·
Maintainers usually reply within 1 day
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
enhancement good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
software-mansion/enriched-markdown#1084 ·
Maintainers usually reply within 2 days