当前位置: 首页 > 知识库问答 >
问题:

此资源不支持请求实体的媒体类型“application/xml”

欧阳德运
2023-03-14

我试图通过XLIFF内容在以下函数的参数,我得到错误:

public IHttpActionResult ProcessSmartlingTranslation([FromBody]xliff xliff)
       {
           return Content(HttpStatusCode.BadRequest, "Any object");
       }

{“message”:“此资源不支持请求实体的媒体类型'application/xml'。”,“exceptionMessage”:“没有MediaTypeFormatter可用于从媒体类型为“application/xml”的内容中读取类型为“xliff”的对象。”,“exceptionType”:“System.Net.Http.UnsupportedMediaTypeException”,“stackTrace”:“在System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent内容,类型,IEnumerable1格式化程序,IFormatterLogger格式化程序记录器,CancellationToken CancellationToken)\r\n在System.Net.Http.HttpContentExtensions.Readasync(HttpContent内容、类型、IEnumerable1格式化程序、IFormatterLogger格式化程序记录器、CancellationToken CancellationToken)\r\n在系统中。网状物Http。模型绑定。FormatterParameterBinding。ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger格式化程序记录器,CancellationToken CancellationToken)“}

我将如何解决这个问题?

完整的代码如下:

// <summary>
   /// XliffModel created
   /// </summary>
   public class xliff
   {
       /// <summary>
       /// file getter setter method
       /// </summary>
       public string file { get; set; }
       /// <summary>
       /// body getter setter method
       /// </summary>
       public string body { get; set; }
   }
   /// <summary>
   /// Web api for smartling translation processing
   /// </summary>
   /// <seealso cref="ApiController" />
   public class SmartlingController : ApiController
   {
       #region Public Methods

       /// <summary>
       /// Get or insert blog comments which is approved based on blogs
       /// </summary>
       /// <remarks> Get or insert blog comments which is approved based on blogs</remarks>
       /// <param name="message">The comment.</param>
       /// <response code="200">Get approved blog comments</response>
       /// <response code="404">Not found</response>
       /// <returns>IHttpActionResult</returns>
       [Route("api/smartling/ProcessSmartlingTranslation")]
       [VersionedRoute("", 1)]
       [ResponseType(typeof(HttpResponseMessage))]
       [HttpPost]
       public IHttpActionResult ProcessSmartlingTranslation([FromBody]xliff xlf)
       {
           return Content(HttpStatusCode.BadRequest, "Any object");
       }
       #endregion
   }

共有1个答案

张英范
2023-03-14

当我检查默认的xml媒体文件格式化程序,它看起来像...

我不知道如果添加手动会发生什么。可能需要自定义序列化。。

            config.Formatters.XmlFormatter
            .SupportedMediaTypes
            .Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/xliff+xml"));
 类似资料:
  • 我有这门课: 这个WEB API方法: 我已经将Web API设置为返回JSON而不是XML,并且我没有对默认配置进行任何其他更改。我试图用火狐的RESTClient扩展来测试这个方法。以下是我的请求: 但是我得到了这个错误: {“Message”:“此资源不支持请求实体的媒体类型“text/plain”,“ExceptionMessage”:“没有MediaTypeFormatter可用于从媒体

  • 我是自动化的一个API的POST调用使用Rest保证和内容类型和ACCEPT头我必须使用"应用程序/vnd.apijson"。但是每次我使用“应用程序/vnd.apijson”,我都会得到415个状态代码。尽管使用邮递员的相同POST呼叫工作得非常好。 以下是我的示例代码: 以下是收到的答复 我曾尝试将Content-Type更改为应用程序/json,正如其他帖子/评论所建议的那样,但这似乎对我的

  • 问题内容: 自数小时以来,我一直在尝试纠正http错误,但它仍显示不支持的页面。我在邮递员中添加标题。 这是我的Java代码 这是我的档案 问题答案: 通过和如何在响应流和请求流之间对对象进行序列化和反序列化。 将会发生的是,将从提供者的注册表中进行搜索,以查找可以处理的媒体类型。如果找不到,则Jersey无法处理该请求,并将发送415不支持的媒体类型。通常,你还应该在服务器端记录一个异常。不知道

  • 我已经创建了一个示例web服务来进行post调用。 我使用的是Jersey JAX-RS和Maven。

  • 我正在用Spring Boot构建一些API,但是当我试图用Postman查询时,我得到了一些关于Content-Type的错误。 我不明白哪里出了问题。 我注意到错误消失时,我删除了@刚体作为参数的方法。为什么啊? 我只想: 将XML发送到API

  • 我已经读过这篇文章,但运气不好。 我正在使用RestShap v106。11.4. 如果我将API的动词从更改为,则会出现错误。该API在Postman中运行完美。 提前谢谢。