415 不支持的媒体类型 - 在 lightswitch 2012 中将 json POST 到 OData 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19396696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:54:24  来源:igfitidea点击:

415 Unsupported Media Type - POST json to OData service in lightswitch 2012

jsonweb-servicespostodatavisual-studio-lightswitch

提问by PaddleFish

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

使用 JSON 发布到 OData 服务时,我收到“错误 415:不支持的媒体类型”。

Solution at the bottom of this rather long post.

这篇相当长的帖子底部的解决方案。

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

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

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

我也可以使用 XML GET/POST 但是我需要使用 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.

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

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

我一直在尝试使用 Fiddler 进行调试,结果如下。

JSON POST

JSON POST

Request

要求

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

Response

回复

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

JSON 获取

Request Header

请求头

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;

Response Header

响应头

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

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.

我不明白为什么当 GET 工作正常时,POST 不支持媒体类型。我意识到我正在使用 DataServiceVersion: 1.0 并且已经查找升级但我使用的是 LightSwitch 2012 并且在引用较新版本时遇到问题而不破坏 LightSwitch 应用程序。我认为 LightSwitch 2013 使用较新的版本 (?) 但是升级对我来说会带来新的(非技术)挑战。我觉得我在圈子里四处走动,这是我最后的手段,在与同事交谈后,我唯一的另一个选择是在数据库上创建数据模型并在此基础上创建 OData 服务。

---- UPDATE----

----更新----

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

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

Using odata=verbose

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

Using DataServiceVersion: 1.0

使用数据服务版本: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>"}}}

Using WebServiceVerion: 2.0

使用 WebService 版本: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>"}}}

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.

我是否正在解决这个问题,这只是 json 请求正文结构不正确的问题吗?我已经尝试了一些没有成功的版本,但是使用 XML 发布是有效的。

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

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

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

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

回答by Jen S

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

看起来这个问题与Content-TypeAccept标题之间的差异有关。在 HTTP 中,Content-Type用于请求和响应负载以传达当前负载的媒体类型。Accept用于请求有效载荷中以说明服务器可能在响应有效载荷中使用的媒体类型。

So, having a Content-Typein 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-Typedoes matter.

因此,Content-Type在没有正文的请求中使用 a(例如您的 GET 请求)是没有意义的。当您执行 POST 请求时,您正在发送消息正文,因此这很Content-Type重要。

If a server is not able to process the Content-Typeof 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 Acceptheader, it will return a 406 error.)

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

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.

在 OData v3 中,媒体类型“application/json”被解释为表示新的 JSON 格式(“JSON light”)。如果服务器不支持读取 JSON light,当看到传入的请求是 JSON light 时会抛出 415 错误。在您的负载中,您的请求正文是详细的 JSON,而不是简单的 JSON,因此服务器应该能够处理您的请求。它不会因为它看到 JSON 轻内容类型。

You could fix this in one of two ways:

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

  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;
    
  1. 在您的 POST 请求中设置内容类型“application/json;odata=verbose”,或
  2. 在请求中包含 DataServiceVersion 标头并将其设置为小于 v3。例如:

    DataServiceVersion: 2.0;
    

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

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