首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从POST/PUT请求获得资源(.NET FHIR客户端,web 2)

无法从POST/PUT请求获得资源(.NET FHIR客户端,web 2)
EN

Stack Overflow用户
提问于 2019-05-27 04:53:26
回答 4查看 1.7K关注 0票数 1

当我尝试使用.NET FHIR向asp.net Web 2服务器发送POST / PUT请求时,我会得到以下错误消息:

Hl7.Fhir.Rest.FhirOperationException:由于 客户端错误(UnsupportedMediaType)。身体没有内容。

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

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

我看了Fiddler,似乎fhir客户端在体内发送了正确的JSON

代码语言:javascript
复制
fhirClient = new FhirClient(serverUrl);
fhirClient.PreferredFormat = ResourceFormat.Json;

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

    var response = fhirClient.Update(patient);

..。

// web 2服务器: WebApiConfig.cs:

代码语言:javascript
复制
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/fhir+json"));

我试过:

代码语言:javascript
复制
[HttpPut]
public void Update([FromBody] Resource resource, string id = null)
{
// updating logic
}

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

但是,当我尝试

代码语言:javascript
复制
[HttpPut]
public void Update([FromBody] object resource, string id = null)
{

我可以在"object“里面看到一个反序列化的病人,并使用jsonParser把它拿回来。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2019-05-27 11:27:01

实际上,也请参阅这里:在HL7 web中将asp.net FHIR序列化为json -这个答案是基于您在上面找到的代码。

票数 0
EN

Stack Overflow用户

发布于 2019-05-27 05:42:57

似乎,我必须写我自己的Fhir MediaTypeFormatter我搜索并找到了这段代码

代码语言:javascript
复制
 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中注册

票数 0
EN

Stack Overflow用户

发布于 2019-06-01 00:03:59

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56319843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档