Unirest 4.x - Duplicate content-type header is being added for request
haroon-sheikh opened this issue · 7 comments
Describe the bug
Duplicate content-type is being added for a request with content-type header and body fields
To Reproduce
Steps to reproduce the behavior:
- When there's
content-type=application/x-www-form-urlencoded
set with body fields - A failing test here #485
Expected behavior
A single content-type header is sent
Screenshots
If applicable, add screenshots to help explain your problem.
Environmental Data:
- Java Version 11
- Version [4.0.3]
Additional context
Add any other context about the problem here.
The additional header is being added from here https://github.com/Kong/unirest-java/blob/main/unirest/src/main/java/kong/unirest/core/java/BodyBuilder.java#L73
Its only duplicate because you added it twice. Don't do that.
Sorry am I missing something, where am I adding it twice? Are you saying, I shouldn't be adding it at all?
Ah I see you've added a check. Thanks.
Unirest has always automatically added the header, in the old 3 line there was a check that didn't auto-add it if it existed, so I pulled that into 4, but Unirest will still let you add multiple headers of the same type IF you want. The HTTP spec says you "shouldn't" do this and then points out exceptions to the rule, so from a tool standpoint, Unirest will let you do it.
Your code in the example really shouldn't add the header and let unirest just add it automatically.
fixed in 4.0.4
Thanks a lot for adding the check. It took me some good time to work out why it was failing.
We use an interceptor to log the request and responses but it looks like the interceptor on
uses the request before the request is transformed and default headers are added so it's not actually what's being executed.