Search code examples
hl7-fhir

FhirClient.CreateAsync returning null instead of OperationOutcome


I have setup a FhirClient to create a new resource.

var clientSettings = FhirClientSettings.CreateDefault();
clientSettings.PreferredReturn = Prefer.OperationOutcome;

var uri = $"https://psims-uat.azure-api.net/adverse-event/fhir";
var fhirClient = new FhirClient(uri, clientSettings, _handler);
return await fhirClient.CreateAsync<AdverseEvent>(adverseEvent);

The code is returning null, instead I want an OperationOutcome so I can extract any warnings and the created resource Id.

Where have I gone wrong?


For completeness _handler is providing my subscription key, as follows:

public class SubscriptionKeyHeaderHandler : HttpClientHandler 
{
    private readonly string subKey;
    private readonly string fhirVersion;

    public SubscriptionKeyHeaderHandler(string subKey, string fhirVersion ) : base()
    {
        this.subKey = subKey;
        this.fhirVersion = fhirVersion;
    }

    protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Add("Ocp-Apim-Subscription-Key", subKey);
        request.Headers.Add("Cache-Control", "no-cache");
        request.Headers.Add("fhir-version", fhirVersion);
        return await base.SendAsync(request, cancellationToken);
    }
}

Solution

  • The FhirClient can only return a resource of the type you create, or null, or throw an exception when an error was returned. So the null you get is actually expected in this case, since there is no AdverseEvent resource and the request was successful.

    What you can do to inspect the OperationOutcome, is look at the fhirClient.LastBodyAsResource. The resource's technical id will also be in the response headers, so that information can be found through fhirClient.LastResult.Location.