I'm trying to set up a little server on my localhost (proof of concept-ish) environment using asp.net core (c#) but keep getting the same error when I visit the 'get' route:
FormatException: Cannot determine type of resource to create from json input data. Is there a 'resourceType' member present? (at path 'line 1, pos 1')
Here's my code:
[Route("Patient/rvrbk")]
public Patient ServeClient()
{
Patient patient = new Patient();
patient.Id = "1";
patient.Gender = AdministrativeGender.Male;
patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
patient.BirthDate = "2004-03-10";
patient.Active = true;
return patient;
}
[Route("get")]
public Patient GetClient()
{
FhirClient client = new FhirClient("http://localhost:54240/");
var patient = client.Read<Patient>("Patient/rvrbk");
return patient;
}
I've tried to set the resourceType on the patient object but that produces an error since it's readonly. I'm sure I'm missing something trivial but can't figure out what.
EDIT
After the answers provided by @AdrianoRepetti I came up with the following setup but ran into another error:
FhirOperationException: Operation was unsuccessful, and returned status OK. OperationOutcome: Overall result: FAILURE (1 errors and 0 warnings) [ERROR] (no details)(further diagnostics: Endpoint returned a body with contentType 'text/plain', while a valid FHIR xml/json body type was expected. Is this a FHIR endpoint?).
Code:
[Route("Patient/rvrbk")]
public string ServeClient()
{
Patient patient = new Patient();
patient.Id = "1";
patient.Gender = AdministrativeGender.Male;
patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
patient.BirthDate = "2004-03-10";
patient.Active = true;
FhirJsonSerializer serializer = new FhirJsonSerializer();
string json = serializer.SerializeToString(patient);
return json;
}
I figured it out.
My first problem was (as pointed out by @Adriano Repetti) that the client didn't know which object it was receiving, this was resolved by running the object trough the SerializeToString([object]) function which is provided by FhirJsonSerializer.
My next problem was that I was returning a string while the client expected 'application/json'. This was resolved by changing the returntype of the 'server' to JsonResult and parse the generated string to json with JObject.Parse([string]).
Code
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Newtonsoft.Json.Linq;
namespace FIHR.Controllers
{
[Route("")]
public class HomeController : Controller
{
public string Index()
{
return "Welkom op de FIHR API";
}
[Route("Patient/rvrbk")]
public IActionResult ServeClient()
{
Patient patient = new Patient();
patient.Id = "1";
patient.Gender = AdministrativeGender.Male;
patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
patient.BirthDate = "2004-03-10";
patient.Active = true;
FhirJsonSerializer serializer = new FhirJsonSerializer();
string jstring = serializer.SerializeToString(patient);
return Content(jstring, "application/json");
}
[Route("get")]
public Patient GetClient()
{
FhirClient client = new FhirClient("http://localhost:54240/"); // http://vonk.fire.ly
Patient patient = client.Read<Patient>("Patient/rvrbk"); // Patient/example
return patient;
}
}
}