415不支持的媒体类型-lightswitch 2012中将json POST到OData服务 [英] 415 Unsupported Media Type - POST json to OData service in lightswitch 2012

查看:99
本文介绍了415不支持的媒体类型-lightswitch 2012中将json POST到OData服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JSON发布到OData服务时,出现错误415:不支持的媒体类型".

I am getting 'error 415: Unsupported Media Type' when posting to an OData service when using JSON.

此较长文章底部的解决方案.

我可以使用JSON进行获取,但是一旦尝试POST,我就会收到此错误.

I can GET using JSON but as soon as I try and POST I get this error.

我也可以使用XML进行GET/POST,但是我需要使用json.

I can also GET/POST using XML however I need to use json.

我认为此错误是指我的标头中的错误,而不是我的请求正文的json格式(在下面也可能是不正确的),我尝试了多种变体导致相同的错误.

I think this error is referring to something wrong in my header, not the json format of my request body which may also be incorrect below, I have tried a number of variations resulting in the same error.

我一直在尝试使用Fiddler进行调试,以下是结果.

I have been trying to debug using Fiddler and the below are the results.

JSON POST

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使用的是较新的版本(?),但对我而言,升级带来了新的(非技术性)挑战. 我觉得我要绕开圈子,这是我的最后选择,与同事交谈后,我唯一的失败选择是在数据库上创建数据模型并在此数据库上创建OData服务.

I don't understany why the media type would be unsupported for POST when GET works fine. I realize I am using DataServiceVersion: 1.0 and have looked up upgrading but I am using LightSwitch 2012 and have had issues referencing the newer version without breaking the LightSwitch app. I think LightSwitch 2013 uses the newer version (?) however upgrading for me opens up new (non technical) challenges. I feel like I am going around in cirlces and this is my last resort and after talking to a colleague my only other option failing this is to create a data model over the database and creating an OData service on this.

---- 更新 ----

---- UPDATE ----

此后,我尝试了Jen S提供的两个修复程序,现在得到错误:400错误的请求.

I have since tried both fixes provided by Jen S and now get Error: 400 Bad Request.

使用odata = verbose

Using 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

Using 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

Using 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进行发布是可行的.

Am i progressing with solving this and is it just a matter of the json request body being structured incorrectly? I have tried a few vriations without success, however posting with XML is working.

----- 解决方案 ------

----- SOLUTION------

感谢Jen的帮助,他使用json格式将其发布到OData服务对我来说是有效的.

Thanks to Jen's help posting using json format to an OData service is working for me.

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"}}

推荐答案

此问题似乎与Content-TypeAccept标头之间的差异有关.在HTTP中,在请求和响应有效负载中使用Content-Type来传达当前有效负载的媒体类型.在请求有效负载中使用Accept来表示服务器在响应有效负载中可以使用的媒体类型.

It looks like this issue has to do with the difference between the Content-Type and Accept headers. In HTTP, Content-Type is used in request and response payloads to convey the media type of the current payload. Accept is used in request payloads to say what media types the server may use in the response payload.

因此,在没有主体的请求中(例如您的GET请求)具有Content-Type毫无意义.当您执行POST请求时,您正在发送消息正文,因此Content-Type确实很重要.

So, having a Content-Type in a request without a body (like your GET request) has no meaning. When you do a POST request, you are sending a message body, so the Content-Type does matter.

如果服务器无法处理请求的Content-Type,它将返回415 HTTP错误. (如果服务器不能满足请求Accept标头中的任何媒体类型,它将返回406错误.)

If a server is not able to process the Content-Type of the request, it will return a 415 HTTP error. (If a server is not able to satisfy any of the media types in the request Accept header, it will return a 406 error.)

在OData v3中,媒体类型"application/json"被解释为表示新的JSON格式("JSON light").如果服务器不支持读取JSON light,则服务器在看到传入的请求是JSON light时将抛出415错误.在有效负载中,您的请求正文是冗长的JSON,而不是JSON light,因此服务器应该能够处理您的请求.只是没有,因为它看到了JSON light内容类型.

In OData v3, the media type "application/json" is interpreted to mean the new JSON format ("JSON light"). If the server does not support reading JSON light, it will throw a 415 error when it sees that the incoming request is JSON light. In your payload, your request body is verbose JSON, not JSON light, so the server should be able to process your request. It just doesn't because it sees the JSON light content type.

您可以通过以下两种方法之一来解决此问题:

You could fix this in one of two ways:

  1. 在POST请求中将内容类型设置为"application/json; odata = verbose",或者
  2. 在请求中包括DataServiceVersion标头,并将其设置为小于v3.例如:

  1. Make the Content-Type "application/json;odata=verbose" in your POST request, or
  2. Include the DataServiceVersion header in the request and set it be less than v3. For example:

DataServiceVersion: 2.0;

(选项2假设您在请求有效负载中未使用任何v3功能.)

(Option 2 assumes that you aren't using any v3 features in your request payload.)

这篇关于415不支持的媒体类型-lightswitch 2012中将json POST到OData服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆