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

后端 未结 1 1324
小蘑菇
小蘑菇 2020-12-15 16:54

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.

相关标签:
1条回答
  • 2020-12-15 17:04

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

    0 讨论(0)
提交回复
热议问题