我在使用JSON时发布到OData服务时收到'错误415:不支持的媒体类型'。
这个相当长的帖子底部的解决方案。
我可以使用JSON获取但是一旦我尝试POST,我就会收到此错误。
我也可以使用XML进行GET / POST,但我需要使用json。
我认为这个错误指的是我的标题中的错误,而不是我的请求正文的json格式,这可能在下面也是错误的,我尝试了一些导致相同错误的变体。
我一直在尝试使用Fiddler进行调试,结果如下。
JSON POST
请求
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
User-Agent: Fiddler
Host: scdb38:8888
Content-Length: 91
Accept: application/json;odata=verbose;
Content-Type: application/json;
{
"d":[
{
"Name":"Great White ",
"Food":"Surfers"
}
]
}
响应
HTTP/1.1 415 Unsupported Media Type
Cache-Control: private
Content-Length: 186
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 16 Oct 2013 06:20:10 GMT
{"error":{"code":"1","message":{"lang":"en-AU","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>Unsupported media type requested.</Message></ExceptionInfo>"}}}
JSON GET
请求标题
GET http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
User-Agent: Fiddler
Host: scdb38:8888
Content-Length: 0
Accept: application/json;odata=verbose;
Content-Type: application/json;
响应标题
HTTP/1.1 200 OK
Cache-Control: no-cache
Content-Length: 591
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 16 Oct 2013 06:23:41 GMT
{"d":[{"__metadata":{"id":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(1)","uri":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(1)","etag":"W/\"X'00000000000007E4'\"","type":"LightSwitchApplication.Shark"},"Id":1,"RowVersion":"AAAAAAAAB+Q=","Name":"Tiger Shark","Food":"Penguins"},{"__metadata":{"id":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(2)","uri":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(2)","etag":"W/\"X'00000000000007E5'\"","type":"LightSwitchApplication.Shark"},"Id":2,"RowVersion":"AAAAAAAAB+U=","Name":"Grey Nurse","Food":"Lettuce"}]}
我没有说明为什么当GET正常工作时,不支持POST的媒体类型。我意识到我正在使用DataServiceVersion:1.0并且已经查找升级但我使用LightSwitch 2012并且在没有破坏LightSwitch应用程序的情况下引用了更新版本的问题。我认为LightSwitch 2013使用的是较新的版本(?),但对我的升级会带来新的(非技术性)挑战。 我觉得我要在cirlces中走动,这是我的最后一招,在与同事交谈之后,我唯一的另一个选择就是在数据库上创建数据模型并在此上创建OData服务。
---- 更新 ----
我已经尝试过Jen S提供的两个修复程序,现在得到Error:400 Bad Request。
使用odata = verbose
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;odata=verbose;
Content-Length: 98
Host: scdb38:8888
{
"d":[
{
"Name":"Great White ",
"Food":"Surfers"
}
]
}
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 201
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 16 Oct 2013 23:31:09 GMT
{"error":{"code":"1","message":{"lang":"en-AU","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>An error occurred while processing this request.</Message></ExceptionInfo>"}}}
使用DataServiceVersion:1.0
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;
Content-Length: 98
Host: scdb38:8888
DataServiceVersion: 1.0;
{
"d":[
{
"Name":"Great White ",
"Food":"Surfers"
}
]
}
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 201
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 16 Oct 2013 23:31:09 GMT
{"error":{"code":"1","message":{"lang":"en-AU","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>An error occurred while processing this request.</Message></ExceptionInfo>"}}}
使用WebServiceVerion:2.0
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;
Content-Length: 98
Host: scdb38:8888
DataServiceVersion: 2.0;
{
"d":[
{
"Name":"Great White ",
"Food":"Surfers"
}
]
}
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 201
Content-Type: application/json;odata=verbose;charset=utf-8
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 16 Oct 2013 23:38:23 GMT
{"error":{"code":"1","message":{"lang":"en-AU","value":"<?xml version=\"1.0\" encoding=\"utf-16\"?><ExceptionInfo><Message>An error occurred while processing this request.</Message></ExceptionInfo>"}}}
我是否正在努力解决这个问题,这只是json请求正文结构不正确的问题?我尝试了一些没有成功的人,但是使用XML发布是有效的。
----- 解决方案 ------
感谢Jen的帮助,使用json格式发布到OData服务正在为我工作。
POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;
Content-Length: 62
Host: scdb38:8888
DataServiceVersion: 1.0;
{
"Name":"Great White ",
"Food":"Surfers"
}
HTTP/1.1 201 Created
Cache-Control: no-cache
Content-Length: 298
Content-Type: application/json;odata=verbose;charset=utf-8
ETag: W/"X'00000000000007E7'"
Location: http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(4)
Server: Microsoft-IIS/7.5
X-Content-Type-Options: nosniff
DataServiceVersion: 1.0;
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 17 Oct 2013 23:22:12 GMT
{"d":{"__metadata":{"id":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(4)","uri":"http://scdb38:8888/BI.Test/ApplicationData.svc/Sharks(4)","etag":"W/\"X'00000000000007E7'\"","type":"LightSwitchApplication.Shark"},"Id":4,"RowVersion":"AAAAAAAAB+c=","Name":"Great White ","Food":"Surfers"}}
答案 0 :(得分:54)
看起来这个问题与Content-Type
和Accept
标头之间的差异有关。在HTTP中,Content-Type
用于请求和响应有效负载,以传达当前有效负载的媒体类型。在请求有效负载中使用Accept
来说明服务器可以在响应有效负载中使用哪些媒体类型。
因此,在没有正文的请求中使用Content-Type
(就像您的GET请求一样)没有任何意义。当您发出POST请求时,您正在发送消息正文,因此Content-Type
确实很重要。
如果服务器无法处理请求的Content-Type
,则会返回415 HTTP错误。 (如果服务器无法满足请求Accept
标头中的任何媒体类型,则会返回406错误。)
在OData v3中,媒体类型“application / json”被解释为新的JSON格式(“JSON light”)。如果服务器不支持读取JSON指示灯,则当它看到传入请求是JSON指示灯时会抛出415错误。在您的有效负载中,您的请求正文是详细的JSON,而不是JSON指示灯,因此服务器应该能够处理您的请求。它只是没有,因为它看到了JSON轻量级内容。
您可以通过以下两种方式解决此问题:
在请求中包含DataServiceVersion标头,并将其设置为小于v3。例如:
DataServiceVersion: 2.0;
(选项2假定您未在请求有效负载中使用任何v3功能。)