Do not send Content-Type header with GET requests
This commit is contained in:
parent
2eee0c7e93
commit
398b46e32d
|
@ -162,7 +162,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
try {
|
try {
|
||||||
T output = (T) client.issueGetResourceRequest(resourceUri,
|
T output = (T) client.issueGetResourceRequest(resourceUri,
|
||||||
withVer(preferredResourceFormat.getHeader(), "5.0"),
|
withVer(preferredResourceFormat.getHeader(), "5.0"),
|
||||||
generateHeaders(),
|
generateHeaders(false),
|
||||||
message,
|
message,
|
||||||
timeoutNormal).getReference();
|
timeoutNormal).getReference();
|
||||||
if (attemptedResourceFormat != preferredResourceFormat) {
|
if (attemptedResourceFormat != preferredResourceFormat) {
|
||||||
|
@ -214,7 +214,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
ResourceRequest<Resource> result = null;
|
ResourceRequest<Resource> result = null;
|
||||||
try {
|
try {
|
||||||
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
|
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);
|
timeoutNormal);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
|
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(),
|
||||||
|
@ -233,7 +233,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
try {
|
try {
|
||||||
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
|
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(false),
|
||||||
"Read " + resourceClass.getName() + "/" + id,
|
"Read " + resourceClass.getName() + "/" + id,
|
||||||
timeoutNormal);
|
timeoutNormal);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
|
@ -251,7 +251,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
try {
|
try {
|
||||||
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndIdAndVersion(resourceClass, id, version),
|
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndIdAndVersion(resourceClass, id, version),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(false),
|
||||||
"VRead " + resourceClass.getName() + "/" + id + "/?_history/" + version,
|
"VRead " + resourceClass.getName() + "/" + id + "/?_history/" + version,
|
||||||
timeoutNormal);
|
timeoutNormal);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
|
@ -269,7 +269,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
try {
|
try {
|
||||||
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndCanonical(resourceClass, canonicalURL),
|
result = client.issueGetResourceRequest(resourceAddress.resolveGetUriFromResourceClassAndCanonical(resourceClass, canonicalURL),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(false),
|
||||||
"Read " + resourceClass.getName() + "?url=" + canonicalURL,
|
"Read " + resourceClass.getName() + "?url=" + canonicalURL,
|
||||||
timeoutNormal);
|
timeoutNormal);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
|
@ -293,7 +293,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
result = client.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resource.getClass(), resource.getId()),
|
result = client.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resource.getClass(), resource.getId()),
|
||||||
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
|
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(true),
|
||||||
"Update " + resource.fhirType() + "/" + resource.getId(),
|
"Update " + resource.fhirType() + "/" + resource.getId(),
|
||||||
timeoutOperation);
|
timeoutOperation);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
|
@ -321,7 +321,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
result = client.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
|
result = client.issuePutRequest(resourceAddress.resolveGetUriFromResourceClassAndId(resourceClass, id),
|
||||||
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
|
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(true),
|
||||||
"Update " + resource.fhirType() + "/" + id,
|
"Update " + resource.fhirType() + "/" + id,
|
||||||
timeoutOperation);
|
timeoutOperation);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
|
@ -357,10 +357,10 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps);
|
URI url = resourceAddress.resolveOperationURLFromClass(resourceClass, name, ps);
|
||||||
if (complex) {
|
if (complex) {
|
||||||
byte[] body = ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true);
|
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);
|
"POST " + resourceClass.getName() + "/$" + name, timeoutLong);
|
||||||
} else {
|
} 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()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
|
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
|
||||||
|
@ -383,7 +383,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
Bundle transactionResult = null;
|
Bundle transactionResult = null;
|
||||||
try {
|
try {
|
||||||
transactionResult = client.postBatchRequest(resourceAddress.getBaseServiceUri(), ByteUtils.resourceToByteArray(batch, false, isJson(getPreferredResourceFormat()), false), withVer(getPreferredResourceFormat(), "4.0"),
|
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()));
|
"transaction", timeoutOperation + (timeoutEntry * batch.getEntry().size()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
handleException("An error occurred trying to process this transaction request", e);
|
handleException("An error occurred trying to process this transaction request", e);
|
||||||
|
@ -398,7 +398,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
try {
|
try {
|
||||||
result = client.issuePostRequest(resourceAddress.resolveValidateUri(resourceClass, id),
|
result = client.issuePostRequest(resourceAddress.resolveValidateUri(resourceClass, id),
|
||||||
ByteUtils.resourceToByteArray(resource, false, isJson(getPreferredResourceFormat()), false),
|
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);
|
"POST " + resourceClass.getName() + (id != null ? "/" + id : "") + "/$validate", timeoutLong);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
|
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"),
|
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ValueSet.class, "expand"),
|
||||||
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
|
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(true),
|
||||||
"ValueSet/$expand?url=" + source.getUrl(),
|
"ValueSet/$expand?url=" + source.getUrl(),
|
||||||
timeoutExpand);
|
timeoutExpand);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -476,7 +476,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
try {
|
try {
|
||||||
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup", params),
|
result = client.issueGetResourceRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup", params),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(false),
|
||||||
"CodeSystem/$lookup",
|
"CodeSystem/$lookup",
|
||||||
timeoutNormal);
|
timeoutNormal);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -495,7 +495,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
result = client.issuePostRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup"),
|
result = client.issuePostRequest(resourceAddress.resolveOperationUri(CodeSystem.class, "lookup"),
|
||||||
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
|
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(true),
|
||||||
"CodeSystem/$lookup",
|
"CodeSystem/$lookup",
|
||||||
timeoutNormal);
|
timeoutNormal);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -514,7 +514,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ConceptMap.class, "translate"),
|
result = client.issuePostRequest(resourceAddress.resolveOperationUri(ConceptMap.class, "translate"),
|
||||||
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
|
ByteUtils.resourceToByteArray(p, false, isJson(getPreferredResourceFormat()), true),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(true),
|
||||||
"ConceptMap/$translate",
|
"ConceptMap/$translate",
|
||||||
timeoutNormal);
|
timeoutNormal);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -539,7 +539,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
|
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
|
||||||
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true),
|
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(true),
|
||||||
"Closure?name=" + name,
|
"Closure?name=" + name,
|
||||||
timeoutNormal);
|
timeoutNormal);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
|
@ -561,7 +561,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
|
result = client.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()),
|
||||||
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true),
|
ByteUtils.resourceToByteArray(params, false, isJson(getPreferredResourceFormat()), true),
|
||||||
withVer(getPreferredResourceFormat(), "4.0"),
|
withVer(getPreferredResourceFormat(), "4.0"),
|
||||||
generateHeaders(),
|
generateHeaders(true),
|
||||||
"UpdateClosure?name=" + name,
|
"UpdateClosure?name=" + name,
|
||||||
timeoutOperation);
|
timeoutOperation);
|
||||||
if (result.isUnsuccessfulRequest()) {
|
if (result.isUnsuccessfulRequest()) {
|
||||||
|
@ -617,7 +617,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
this.headers = headers;
|
this.headers = headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Headers generateHeaders() {
|
private Headers generateHeaders(boolean hasBody) {
|
||||||
Headers.Builder builder = new Headers.Builder();
|
Headers.Builder builder = new Headers.Builder();
|
||||||
// Add basic auth header if it exists
|
// Add basic auth header if it exists
|
||||||
if (basicAuthHeaderExists()) {
|
if (basicAuthHeaderExists()) {
|
||||||
|
@ -635,7 +635,7 @@ public class FHIRToolingClient extends FHIRBaseToolingClient {
|
||||||
builder.add("Accept-Language: "+acceptLang);
|
builder.add("Accept-Language: "+acceptLang);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Utilities.noString(contentLang)) {
|
if (hasBody && !Utilities.noString(contentLang)) {
|
||||||
builder.add("Content-Language: "+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;
|
org.hl7.fhir.r5.utils.client.network.ResourceRequest<Resource> result = null;
|
||||||
try {
|
try {
|
||||||
result = client.issueGetResourceRequest(resourceAddress.resolveGetResource(resourceClass, id),
|
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) {
|
} catch (IOException e) {
|
||||||
throw new FHIRException(e);
|
throw new FHIRException(e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.EFhirClientException;
|
||||||
import org.hl7.fhir.r5.utils.client.ResourceFormat;
|
import org.hl7.fhir.r5.utils.client.ResourceFormat;
|
||||||
import org.hl7.fhir.utilities.MimeType;
|
import org.hl7.fhir.utilities.MimeType;
|
||||||
|
import org.hl7.fhir.utilities.Utilities;
|
||||||
import org.hl7.fhir.utilities.settings.FhirSettings;
|
import org.hl7.fhir.utilities.settings.FhirSettings;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
@ -91,7 +92,9 @@ public class FhirRequestBuilder {
|
||||||
*/
|
*/
|
||||||
protected static void addResourceFormatHeaders(Request.Builder request, String format) {
|
protected static void addResourceFormatHeaders(Request.Builder request, String format) {
|
||||||
request.addHeader("Accept", 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,10 +26,27 @@ class FhirRequestBuilderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Test resource format headers are added correctly.")
|
@DisplayName("Test resource format headers are added correctly (GET).")
|
||||||
void addResourceFormatHeaders() {
|
void addResourceFormatHeadersGET() {
|
||||||
String testFormat = "yaml";
|
String testFormat = "yaml";
|
||||||
Request.Builder request = new Request.Builder().url("http://www.google.com");
|
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);
|
FhirRequestBuilder.addResourceFormatHeaders(request, testFormat);
|
||||||
|
|
||||||
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
|
Map<String, List<String>> headersMap = request.build().headers().toMultimap();
|
||||||
|
|
Loading…
Reference in New Issue