在一次对接API过程中,调试接口时PostMan报"415Unsupported Media Type"状态码,搞了好几天没通,API响应结果始终是415,下面是我在PostMan中的配置:
响应的Json数据:
{
"Message": "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'AvailableRatesRequest' from content with media type 'application/octet-stream'.",
"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
"StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}
有一天早上我试着将Body Raw中的Json数据删了,诶~,PostMan不再提示415了,状态码也变成了"200 OK"了,从API返回的Json信息可以看出是缺少参数,根据提示信息试着将参数配置到"x-www-form-urlencoded"中,没毛病,剩下的就是Json中的数据的问题了
总结一下一直调不通的原因:
API文档上面提到URL、Header、Method应该怎么配置,并未提及Json应该放在哪个位置,好在他们那边有提示信息,不然谁知道这Json应该怎么配置,怕只有API开发商那边才知道,所以在调试这些接口时如果调不通可以试着一个一个的删除配置信息,来缩小产生这个错误的原因。如果试过了所有方法还是调不通,那唯一的办法就是咨询API开发商了。
由于出现http 415的原因很多,因此以上解决方法并不一定能解决所有415问题