Steve Borman
Steve Borman

Reputation: 375

Getting an error when trying to use the FhirClient to create a Binary resource

This line of code fails:

var uploadResult = await fhirClient.CreateAsync(binary);

With an error:

Hl7.Fhir.Rest.FhirOperationException: 'Operation was unsuccessful because of a client error (UnsupportedMediaType). 

However, the output of binary.ToJson() is:

{
"resourceType": "Binary",
"contentType": "application/pdf",
"data": "H4sIAAAAAAAEAOy7V4vE3NYm9le....."}

Which works perfectly if I post using Postman.

Any ideas why my FhirClient fails?

Upvotes: 0

Views: 419

Answers (1)

Ryan Schulteis
Ryan Schulteis

Reputation: 41

If you are using dotnet it may be that the fhirClient expects the data field to be a byte array, and performs the conversion to base64 "under the hood" within the CreateAsync method. Here is a working example:

using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;

namespace myfhir
{
  class Program
  {
    static async System.Threading.Tasks.Task Main(string[] args)
    {
      var client = new FhirClient("http://hapi.fhir.org/baseR4");
      client.Settings.PreferredFormat = ResourceFormat.Json;

      Byte[] bytes = File.ReadAllBytes("test.pdf");
      Binary myBinary = new Binary();
      myBinary.ContentType = "application/pdf";
      myBinary.Data = bytes;

      var result = await client.CreateAsync<Binary>(myBinary);

      Console.WriteLine(result.Id);
    }
  }
}

The result is available at http://hapi.fhir.org/baseR4/Binary/<result.Id>

Appendix

My *.csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Hl7.Fhir.R4" Version="5.3.0" />
  </ItemGroup>

</Project>

Upvotes: 1

Related Questions