无法从POST / PUT请求中获取资源(.NET FHIR客户端,Web API 2)

时间:2019-05-27 04:53:26

标签: asp.net-web-api2 hl7-fhir

当我尝试使用.NET FHIR API向asp.net Web API 2服务器发送POST / PUT请求时,出现错误消息:

  

Hl7.Fhir.Rest.FhirOperationException:由于操作失败,
  客户端错误(UnsupportedMediaType)。身体没有任何内容。

1)我需要创建某种MediaType处理程序/格式化程序吗?

2)是否有一个开放源代码服务器,其中的代码实现了.NET FHIR API的最佳实践?

我看着Fiddler,看来这是客户端在体内发送正确的JSON

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

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

    var response = fhirClient.Update(patient);

...
// Web api 2服务器: WebApiConfig.cs:

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

我尝试过:

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

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

但是,当我尝试

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

我可以在“对象”内部看到一个反序列化的患者,并使用jsonParser将其取回

3 个答案:

答案 0 :(得分:0)

似乎,我必须编写自己的Fhir MediaTypeFormatter 我用Google搜索并发现了this code

 public class JsonFhirFormatter : FhirMediaTypeFormatter
    {
        private readonly FhirJsonParser _parser = new FhirJsonParser();
        private readonly FhirJsonSerializer _serializer = new FhirJsonSerializer();

        public JsonFhirFormatter() : base()
        {
            foreach (var mediaType in ContentType.JSON_CONTENT_HEADERS)
                SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
        }

        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
        {
            base.SetDefaultContentHeaders(type, headers, mediaType);
            headers.ContentType = FhirMediaType.GetMediaTypeHeaderValue(type, ResourceFormat.Json);
        }

        public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
        {
            try
            {
                var body = base.ReadBodyFromStream(readStream, content);

                if (typeof(Resource).IsAssignableFrom(type))
                {
                    Resource resource = _parser.Parse<Resource>(body);
                    return Task.FromResult<object>(resource);
                }
                else
                {
                    throw Error.Internal("Cannot read unsupported type {0} from body", type.Name);
                }
            }
            catch (FormatException exception)
            {
                throw Error.BadRequest("Body parsing failed: " + exception.Message);
            }
        }

        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
        {
            using(StreamWriter streamwriter = new StreamWriter(writeStream))
            using (JsonWriter writer = new JsonTextWriter(streamwriter))
            {
                SummaryType summary = requestMessage.RequestSummary();

                if (type == typeof(OperationOutcome))
                {
                    Resource resource = (Resource)value;
                    _serializer.Serialize(resource, writer, summary);
                }
                else if (typeof(Resource).IsAssignableFrom(type))
                {
                    Resource resource = (Resource)value;
                    _serializer.Serialize(resource, writer, summary);
                }
                else if (typeof(FhirResponse).IsAssignableFrom(type))
                {
                    FhirResponse response = (value as FhirResponse);
                    if (response.HasBody)
                    {
                        _serializer.Serialize(response.Resource, writer, summary);
                    }
                }
            }

            return Task.CompletedTask;
        }
    }

并将其注册到Global.asax

答案 1 :(得分:0)

实际上,也请参见此处:HL7 FHIR serialisation to json in asp.net web api-该答案基于您在上面找到的代码。

答案 2 :(得分:0)

其中有几个选项,包括github上的开源fhir-net-web-api项目。 有一个aspnetcore版本和一个完整的框架版本。 检查其中的示例项目以了解如何使用它们。