Kong/unirest-java

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:

  1. When there's content-type=application/x-www-form-urlencoded set with body fields
  2. 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.

ryber commented

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.

ryber commented

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.

ryber commented

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

config.getUniInterceptor().onRequest(request, config);
uses the request before the request is transformed and default headers are added so it's not actually what's being executed.