Search code examples
asp.net-web-api2hl7-fhir

Can't get Resource from POST/PUT request (.NET FHIR client, web api 2)


When I try to send a POST / PUT request using .NET FHIR API to asp.net Web API 2 server, I get the error message:

Hl7.Fhir.Rest.FhirOperationException: the operation failed due to a
client error (UnsupportedMediaType). The body has no content.

1) Do I need to create some kind of MediaType Handler / Formatter?

2) Is there an open source server in which code implements the best practices of the .NET FHIR API?

I looked at Fiddler, seems fhir client sends correct JSON in the body

fhirClient = new FhirClient(serverUrl);
fhirClient.PreferredFormat = ResourceFormat.Json;

Patient patient = new Patient();
//fill patient

    var response = fhirClient.Update(patient);

...
// web api 2 server: WebApiConfig.cs:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/fhir+json"));

I tried:

[HttpPut]
public void Update([FromBody] Resource resource, string id = null)
{
// updating logic
}

//or
[HttpPut]
public void Update(Resource resource, string id = null)
{
// updating logic
}

but, when I tried

[HttpPut]
public void Update([FromBody] object resource, string id = null)
{

I can see inside "object" a deserialized Patient and use jsonParser to get it back


Solution

  • Indeed, see also here: HL7 FHIR serialisation to json in asp.net web api - that answer is based on the code you found above.