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

1

29

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"}}
Sabbatical answered 16/10, 2013 at 6:35 Comment(0)
E
67

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

Expression answered 16/10, 2013 at 7:44 Comment(8)
Thanks Jen S for the detailed and logical response, it makes perfect sense. I have tried both fixes above and both result in an error 400: Bad Request. I am not sure if this is making progress, could it be a problem now with the format of the json request body I am sending through? I will try and put the response text in the above post.Sabbatical
Yes, it is making progress :). In JSON Verbose request payloads there should not be a "d" outer element. This is for responses only. And I believe you can only post one element at a time, so you shouldn't have an array in the request JSON either. Are you using a library, or crafting these by hand? If you're using a language that has an OData Library, I would suggest using that instead of hand-crafting the payloads. (But not all languages have good libraries at the moment :(.)Expression
That worked! Thanks Jen for all your help. I have posted the final working request and responses above for anyone else who stumbles upon this post. If I had more reputation I would give you an upvote.Sabbatical
@JenS : hey, could you please look at this (#34566507) question as well. It's on the same lines. Thanks :)Bequeath
My 2 cents... Watch how you spell "content type". I had spelled mine in Fiddler as ContentType: application/json instead of Content-Type: application/json. The second I used the correct spelling, I was able to access my web api via filder instead of getting the 'Unsupported Media Type' error.Vagarious
thanks here the error tools.ietf.org/html/rfc7231#section-6.5.13Barone
I think Fiddler should automatically add Content-Type: application/json to a post or create a popup requesting to know the Content-Type, otherwise the 415 error is thrownDecern
Also, be aware that the Content-Type header should not end with a semicolon.Sepulture

© 2022 - 2024 — McMap. All rights reserved.