Skip to content
This repository has been archived by the owner on Jul 2, 2023. It is now read-only.

Commit

Permalink
Ensure request content-type is preserved over the entity. (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
budjb authored May 20, 2020
1 parent 194874d commit 32a9b06
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
version=2.0.6
version=2.0.7
group=com.budjb
githubHttpsUrl=https://github.com/budjb/http-requests
githubGitUrl=git@github.com:budjb/http-request.git
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected HttpResponse execute(HttpContext context, HttpEntity httpEntity, HttpC
}

connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", httpEntity.getFullContentType());
connection.setRequestProperty("Content-Type", request.getHeaders().containsKey("Content-Type") ? request.getHeaders().getFlat("Content-Type") : httpEntity.getFullContentType());

OutputStream outputStream = filterProcessor.filterOutputStream(connection.getOutputStream());
StreamUtils.shovel(httpEntity.getInputStream(), outputStream);
Expand Down
6 changes: 6 additions & 0 deletions http-requests-documentation/src/docs/changelog.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The changelog will be updated with release notes for each release of the library.

2.0.7::

* Ensure that a Content-Type specified in the request is preferred over the one provided
in the HTTP entity. This ensures that any Content-Type provided by a converter will not take
precedence over a user-specified one.
2.0.6::

* Make the build compatible with JDK 8.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,18 @@ abstract class HttpIntegrationTestSuiteSpec extends AbstractIntegrationSpec {
!((List) response.get('x-foo'))[0]
}

def 'A Content-Type contained in the request is preferred over the one in the entity'() {
setup:
HttpRequest request = new HttpRequest("${baseUrl}/echo")
request.setHeader('content-type', 'foo/bar')

when:
def response = httpClientFactory.createHttpClient().post(request, 'foo')

then:
response.getHeader('content-type') == 'foo/bar'
}

static class CloseableFilter implements HttpClientFilter, Closeable {
boolean closed = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected HttpResponse execute(HttpContext context, HttpEntity entity, HttpClien

builder = applyHeaders(builder, request.getHeaders());

if (entity != null && entity.getFullContentType() != null) {
if (!request.getHeaders().containsKey("Content-Type") && entity != null && entity.getFullContentType() != null) {
builder = builder.type(entity.getFullContentType());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ protected HttpResponse execute(HttpContext context, HttpEntity httpEntity, HttpC

Entity<InputStream> entity = null;
if (httpEntity != null) {
entity = Entity.entity(httpEntity.getInputStream(), MediaType.valueOf(httpEntity.getFullContentType()));
entity = Entity.entity(httpEntity.getInputStream(), MediaType.valueOf(
request.getHeaders().containsKey("Content-Type") ? request.getHeaders().getFlat("Content-Type") : httpEntity.getFullContentType()
));
}

Response clientResponse;
Expand Down

0 comments on commit 32a9b06

Please sign in to comment.