Skip to content

Commit

Permalink
Merge pull request #240 from /issues/239-content-type-per-request
Browse files Browse the repository at this point in the history
Fix #239: RestClient content type per request
  • Loading branch information
banterCZ authored Dec 4, 2023
2 parents cd1390f + 890d0e8 commit a1fb203
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public <T> ResponseEntity<T> post(String path, Object request, MultiValueMap<Str
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
return buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -398,7 +398,7 @@ public <T> void postNonBlocking(String path, Object request, MultiValueMap<Strin
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -444,7 +444,7 @@ public <T> ResponseEntity<T> put(String path, Object request, MultiValueMap<Stri
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
return buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -472,7 +472,7 @@ public <T> void putNonBlocking(String path, Object request, MultiValueMap<String
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -586,7 +586,7 @@ public <T> ResponseEntity<T> patch(String path, Object request, MultiValueMap<St
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
return buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -614,7 +614,7 @@ public <T> void patchNonBlocking(String path, Object request, MultiValueMap<Stri
h.addAll(headers);
}
})
.contentType(config.getContentType())
.contentType(resolveContentType(config, headers))
.accept(config.getAcceptType());
buildRequest(spec, request)
.exchangeToMono(rs -> handleResponse(rs, responseType))
Expand Down Expand Up @@ -770,6 +770,15 @@ private <T> Mono<ResponseEntity<T>> handleResponse(ClientResponse response, Para
});
}

private static MediaType resolveContentType(final RestClientConfiguration config, final MultiValueMap<String, String> headers) {
if (headers != null && headers.containsKey(HttpHeaders.CONTENT_TYPE)) {
final MediaType contentType = MediaType.valueOf(headers.getFirst(HttpHeaders.CONTENT_TYPE));
logger.debug("Overriding content type {} from config by {} from the given headers", config.getContentType(), contentType);
return contentType;
}
return config.getContentType();
}

/**
* In case base URL is specified, append the path to complete the URL. Otherwise use the path as the URL specification.
* @param uriSpec Request headers URI specification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,23 @@ void testPostWithMultipartData() throws RestClientException {
assertEquals(requestData, responseEntity.getBody().getResponseObject().getResponse());
}

@Test
void testPostOctetStream() throws Exception {
final byte[] request = {1, 2};

final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

final ResponseEntity<ObjectResponse<TestResponse>> responseEntity =
restClient.post("/octet-stream", request, null, headers, new ParameterizedTypeReference<>(){});

assertNotNull(responseEntity);
assertNotNull(responseEntity.getBody());
assertNotNull(responseEntity.getBody().getResponseObject());
assertEquals("OK", responseEntity.getBody().getStatus());
assertEquals("length: 2", responseEntity.getBody().getResponseObject().getResponse());
}

@Test
void testPostWithLargeServerResponse() {
final Logger defaultRestClientLogger = (Logger) LoggerFactory.getLogger(DefaultRestClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import io.getlime.core.rest.model.base.request.ObjectRequest;
import io.getlime.core.rest.model.base.response.ObjectResponse;
import io.getlime.core.rest.model.base.response.Response;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.net.URI;
import java.util.Arrays;
import java.util.Enumeration;
Expand Down Expand Up @@ -85,6 +85,12 @@ public ObjectResponse<TestResponse> testPostWithMultipartRequestAndResponse(@Req
return new ObjectResponse<>(testResponse);
}

@PostMapping(value = "/octet-stream", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ObjectResponse<TestResponse> testPostOctetStream(@RequestBody byte[] request) {
final TestResponse testResponse = new TestResponse("length: " + request.length);
return new ObjectResponse<>(testResponse);
}

@PostMapping("/object-response-large")
public ObjectResponse<TestResponse> testPostWithLargeServerResponse() {
TestResponse testResponse = new TestResponse(Arrays.toString(new byte[10 * 1024 * 1024]));
Expand Down

0 comments on commit a1fb203

Please sign in to comment.