Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.58k stars 591 forks source link

Unirest 4.x - Duplicate content-type header is being added for request #486

Closed haroon-sheikh closed 11 months ago

haroon-sheikh commented 11 months ago

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 https://github.com/Kong/unirest-java/pull/485

Expected behavior A single content-type header is sent

Screenshots If applicable, add screenshots to help explain your problem.

Environmental Data:

Additional context Add any other context about the problem here.

haroon-sheikh commented 11 months ago

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

ryber commented 11 months ago

Its only duplicate because you added it twice. Don't do that.

haroon-sheikh commented 11 months ago

Sorry am I missing something, where am I adding it twice? Are you saying, I shouldn't be adding it at all?

haroon-sheikh commented 11 months ago

Ah I see you've added a check. Thanks.

ryber commented 11 months ago

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 11 months ago

fixed in 4.0.4

haroon-sheikh commented 11 months ago

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 https://github.com/Kong/unirest-java/blob/09c2e8bd1bcb7dfbb5b03a29dbd64261ffa992dc/unirest/src/main/java/kong/unirest/core/java/JavaClient.java#L65 uses the request before the request is transformed and default headers are added so it's not actually what's being executed.