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

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

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.

I can also GET/POST using XML however I need to use 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.

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

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

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.

---- UPDATE ----

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

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

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

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

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

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

This question is related to json web-services post odata visual-studio-lightswitch

The answer is


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.

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.

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.)

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. 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;
    

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


Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?

Examples related to web-services

How do I POST XML data to a webservice with Postman? How to send json data in POST request using C# org.springframework.web.client.HttpClientErrorException: 400 Bad Request How to call a REST web service API from JavaScript? The request was rejected because no multipart boundary was found in springboot Generating Request/Response XML from a WSDL How to send a POST request using volley with string body? How to send post request to the below post method using postman rest client How to pass a JSON array as a parameter in URL Postman Chrome: What is the difference between form-data, x-www-form-urlencoded and raw

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time

Examples related to odata

Could not load file or assembly 'System.Web.Http 4.0.0 after update from 2012 to 2013 415 Unsupported Media Type - POST json to OData service in lightswitch 2012 Could not load file or assembly System.Net.Http, Version=4.0.0.0 with ASP.NET (MVC 4) Web API OData Prerelease Difference between OData and REST web services

Examples related to visual-studio-lightswitch

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