Resolving 415 Unsupported Media Type Error When POSTing JSON to OData Service in LightSwitch 2012

Nov 25, 2025 · Programming · 10 views · 7.8

Keywords: OData | JSON | LightSwitch | HTTP Headers | 415 Error

Abstract: This article provides an in-depth analysis of the 415 Unsupported Media Type error encountered when POSTing JSON requests to OData services in LightSwitch 2012. By comparing the differences between GET and POST requests, it explains the distinct roles of Content-Type and Accept headers, and presents two effective solutions: modifying Content-Type to application/json;odata=verbose or adding the DataServiceVersion header. With detailed code examples, the article demonstrates how to properly configure request headers and JSON data format to successfully implement JSON POST operations.

Problem Background and Phenomenon Analysis

When developing applications with LightSwitch 2012, many developers encounter a common issue: GET requests to OData services return JSON data normally, but POST requests result in a <span style="font-family: monospace;">415 Unsupported Media Type</span> error. This seemingly contradictory phenomenon actually highlights the importance of proper HTTP header configuration.

From the provided debugging data, we can observe that although GET requests include the <span style="font-family: monospace;">Content-Type: application/json</span> header, this header is effectively ignored by the server since GET requests typically don't contain a message body. However, POST requests must include a message body, making the <span style="font-family: monospace;">Content-Type</span> header critically important as it informs the server about the media type format of the request body.

In-depth Analysis of HTTP Header Mechanisms

In the HTTP protocol specification, <span style="font-family: monospace;">Content-Type</span> and <span style="font-family: monospace;">Accept</span> headers serve different purposes:

When a server cannot process the <span style="font-family: monospace;">Content-Type</span> specified in a request, it returns a 415 status code. Conversely, if the server cannot satisfy any of the media types specified in the <span style="font-family: monospace;">Accept</span> header, it returns a 406 status code.

OData Version Compatibility Issues

In the OData v3 specification, the media type <span style="font-family: monospace;">application/json</span> is interpreted as the new JSON Light format. If the server side doesn't support reading JSON Light format, but the client uses this format to make requests, a 415 error will be triggered.

From the JSON data format in the problem description, we can see that verbose JSON format is actually being used, not JSON Light format:

{
   "d":[
      {
         "Name":"Great White ",
         "Food":"Surfers"
      }
   ]
}

This format should be processable by the server correctly. The problem arises from improper header configuration causing the server to misjudge the data format.

Solution Implementation

Based on our in-depth analysis of the problem, we provide two effective solutions:

Solution 1: Modify Content-Type Header

Explicitly specify the <span style="font-family: monospace;">Content-Type</span> in POST requests as verbose JSON format:

Content-Type: application/json;odata=verbose;

This clearly informs the server to use OData verbose JSON format to process the request body, avoiding format misjudgment.

Solution 2: Add DataServiceVersion Header

Include the data service version header in the request, setting it to v2.0 or lower:

DataServiceVersion: 2.0;

This approach assumes that the request payload doesn't contain any v3 feature functionalities. By specifying a lower version number, it ensures the server uses compatible JSON format processing logic.

Code Examples and Debugging Process

Let's demonstrate the correct configuration method through specific code examples. First, the incorrect configuration:

POST http://scdb38:8888/bi.test/applicationdata.svc/Sharks HTTP/1.1
Accept: application/json;odata=verbose;
Content-Type: application/json;
Content-Length: 91
Host: scdb38:8888

{
   "d":[
      {
         "Name":"Great White ",
         "Food":"Surfers"
      }
   ]
}

This configuration will cause a 415 error. The corrected proper configuration is as follows:

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

It's important to note that in the successful solution, the JSON data format was also adjusted by removing the outer <span style="font-family: monospace;">"d"</span> wrapper and using object format directly.

Practical Recommendations and Considerations

In actual development practice, we recommend developers to:

  1. Use debugging tools like Fiddler to carefully examine request and response headers
  2. Ensure consistency between JSON data format and header declarations
  3. Pay attention to backward compatibility issues when upgrading OData versions
  4. For older versions like LightSwitch 2012, prioritize using Solution 1 for fixes

Through proper header configuration and data format adjustments, developers can successfully implement JSON POST operations to OData services in LightSwitch 2012, avoiding the困扰 of 415 Unsupported Media Type errors.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.