Do not send Content-Type header with GET requests

This commit is contained in:
Grahame Grieve 2024-06-01 22:55:35 +10:00
parent 2eee0c7e93
commit 398b46e32d
3 changed files with 43 additions and 23 deletions

View File

@ -162,7 +162,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
try {
T output = (T) client.issueGetResourceRequest(resourceUri,
withVer(preferredResourceFormat.getHeader(), "5.0"),
generateHeaders(),
generateHeaders(false),
message,
timeoutNormal).getReference();
if (attemptedResourceFormat != preferredResourceFormat) {
@ -214,7 +214,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
ResourceRequest<Resource> result = null;
try {
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(), "Read " + resourceClass + "/" + id,
withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(false), "Read " + resourceClass + "/" + id,
timeoutNormal);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
@ -233,7 +233,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
try {
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(false),
"Read " + resourceClass.getName() + "/" + id,
timeoutNormal);
if (result.isUnsuccessfulRequest()) {
@ -251,7 +251,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
try {
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndIdAndVersion(resourceClass, id, version),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(false),
"VRead " + resourceClass.getName() + "/" + id + "/?_history/" + version,
timeoutNormal);
if (result.isUnsuccessfulRequest()) {
@ -269,7 +269,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
try {
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndCanonical(resourceClass, canonicalURL),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(false),
"Read " + resourceClass.getName() + "?url=" + canonicalURL,
timeoutNormal);
if (result.isUnsuccessfulRequest()) {
@ -293,7 +293,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
result = client.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resource.getClass(), resource.getId()),
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"Update " + resource.fhirType() + "/" + resource.getId(),
timeoutOperation);
if (result.isUnsuccessfulRequest()) {
@ -321,7 +321,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
result = client.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"Update " + resource.fhirType() + "/" + id,
timeoutOperation);
if (result.isUnsuccessfulRequest()) {
@ -357,10 +357,10 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps);
if (complex) {
byte[] body = ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true);
result = client.issuePostRequest(url, body, withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(),
result = client.issuePostRequest(url, body, withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(true),
"POST " + resourceClass.getName() + "/$" + name, timeoutLong);
} else {
result = client.issueGetResourceRequest(url, withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(), "GET " + resourceClass.getName() + "/$" + name, timeoutLong);
result = client.issueGetResourceRequest(url, withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(false), "GET " + resourceClass.getName() + "/$" + name, timeoutLong);
}
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
@ -383,7 +383,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
Bundle transactionResult = null;
try {
transactionResult = client.postBatchRequest(resourceAddress.getBaseServiceUri(), ByteUtils.resourceToByteArray(batch, false, isJson(getPreferredResourceFormat()), false), withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"transaction", timeoutOperation + (timeoutEntry * batch.getEntry().size()));
} catch (Exception e) {
handleException("An error occurred trying to process this transaction request", e);
@ -398,7 +398,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
try {
result = client.issuePostRequest(resourceAddress.resolveValidateUri(resourceClass, id),
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(),
withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(true),
"POST " + resourceClass.getName() + (id != null ? "/" + id : "") + "/$validate", timeoutLong);
if (result.isUnsuccessfulRequest()) {
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
@ -458,7 +458,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand"),
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"ValueSet/$expand?url=" + source.getUrl(),
timeoutExpand);
} catch (IOException e) {
@ -476,7 +476,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
try {
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup", params),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(false),
"CodeSystem/$lookup",
timeoutNormal);
} catch (IOException e) {
@ -495,7 +495,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup"),
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"CodeSystem/$lookup",
timeoutNormal);
} catch (IOException e) {
@ -514,7 +514,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ConceptMap.class, "translate"),
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"ConceptMap/$translate",
timeoutNormal);
} catch (IOException e) {
@ -539,7 +539,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"Closure?name=" + name,
timeoutNormal);
if (result.isUnsuccessfulRequest()) {
@ -561,7 +561,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true),
withVer(getPreferredResourceFormat(), "4.0"),
generateHeaders(),
generateHeaders(true),
"UpdateClosure?name=" + name,
timeoutOperation);
if (result.isUnsuccessfulRequest()) {
@ -617,7 +617,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
this.headers = headers;
}
private Headers generateHeaders() {
private Headers generateHeaders(boolean hasBody) {
Headers.Builder builder = new Headers.Builder();
// Add basic auth header if it exists
if (basicAuthHeaderExists()) {
@ -635,7 +635,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
builder.add("Accept-Language: "+acceptLang);
}
if (!Utilities.noString(contentLang)) {
if (hasBody && !Utilities.noString(contentLang)) {
builder.add("Content-Language: "+contentLang);
}
@ -689,7 +689,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
org.hl7.fhir.r5.utils.client.network.ResourceRequest<Resource> result = null;
try {
result = client.issueGetResourceRequest(resourceAddress.resolveGetResource(resourceClass, id),
withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(), resourceClass.getName()+"/"+id, timeoutNormal);
withVer(getPreferredResourceFormat(), "4.0"), generateHeaders(false), resourceClass.getName()+"/"+id, timeoutNormal);
} catch (IOException e) {
throw new FHIRException(e);
}

View File

@ -13,6 +13,7 @@ import org.hl7.fhir.r5.utils.ResourceUtilities;
import org.hl7.fhir.r5.utils.client.EFhirClientException;
import org.hl7.fhir.r5.utils.client.ResourceFormat;
import org.hl7.fhir.utilities.MimeType;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.settings.FhirSettings;
import javax.annotation.Nonnull;
@ -91,7 +92,9 @@ public class FhirRequestBuilder {
*/
protected static void addResourceFormatHeaders(Request.Builder request, String format) {
request.addHeader("Accept", format);
request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
if (Utilities.existsInList(request.getMethod$okhttp(), "POST", "PUT")) {
request.addHeader("Content-Type", format + ";charset=" + DEFAULT_CHARSET);
}
}
/**

View File

@ -26,10 +26,27 @@ class FhirRequestBuilderTest {
}
@Test
@DisplayName("Test resource format headers are added correctly.")
void addResourceFormatHeaders() {
@DisplayName("Test resource format headers are added correctly (GET).")
void addResourceFormatHeadersGET() {
String testFormat = "yaml";
Request.Builder request = new Request.Builder().url("http://www.google.com");
request.setMethod$okhttp("GET");
FhirRequestBuilder.addResourceFormatHeaders(request, testFormat);
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
Assertions.assertNotNull(headersMap.get("Accept"), "Accept header null.");
Assertions.assertEquals(testFormat, headersMap.get("Accept").get(0),
"Accept header not populated with expected value " + testFormat + ".");
Assertions.assertNull(headersMap.get("Content-Type"), "Content-Type header not null.");
}
@Test
@DisplayName("Test resource format headers are added correctly (POST).")
void addResourceFormatHeadersPOST() {
String testFormat = "yaml";
Request.Builder request = new Request.Builder().url("http://www.google.com");
request.setMethod$okhttp("POST");
FhirRequestBuilder.addResourceFormatHeaders(request, testFormat);
Map<String, List<String>> headersMap = request.build().headers().toMultimap();