Search code examples
jsonentity-frameworkwcf-data-services

Posting to a WCF Data Service 5


I am trying to POST to a EntityFramework backed WCF Data Service but I am getting:

415 Unsupported Media Type

I followed this guide to put the headers into my jQuery POST http://blogs.msdn.com/b/astoriateam/archive/2012/04/11/what-happened-to-application-json-in-wcf-ds-5-0.aspx

These are my headers:

POST /webservices/service/service.svc/Activities HTTP/1.1
Host: www.url.com
Connection: keep-alive
Content-Length: 138
Origin: http://www.url.com
X-Requested-With: XMLHttpRequest
MaxDataServiceVersion: 3.0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5
Content-Type: application/json; charset=UTF-8
Accept: application/json;odata=verbose
Referer: http://www.url.com/sites/site/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

This site helped with using EntityFramework with WCF Data Services - http://blogs.msdn.com/b/writingdata_services/archive/2011/06/15/entity-framework-4-1-code-first-and-wcf-data-services.aspx

function AddActivity() {
    var activity = {
        activity:
            {
                "Title": "Test From Code",
                "Detail": "Code Example",
                "Started": "2012-06-21T09:00:00",
                "UserId": 17
            }
    };

    var url = 'http://www.url.com/webservices/service/service.svc/Activities';

    $.ajax({
        type: "POST",
        url: url,
        data: activity,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Accept", "application/json;odata=verbose");
            xhr.setRequestHeader("MaxDataServiceVersion", "3.0");
        },
        success: function (data) {
            alert('Success');
        },
        error: function (err) {
            alert('Fail\n' + err.statusText);
        }
    });
}

I can read data JSON data OK


Solution

  • WCF Data Services 5.0 does not support application/json content type for request (or response) payloads if the payload has version V3 (DataServiceVersion). In the above case you didn't specify the DataServiceVersion (which you always should by the way). The server in such case has to infer the version somehow, the WCF Data Service server will assume the maximum version it and the client understands. Since the server itself understands V3 and you specified MaxDataServiceVersion: 3.0 which means the client also understands V3 it assumes the payload is a V3 payload.

    V3 payloads currently don't support application/json, they only support application/json;odata=verbose.

    See this blog post for more explanation: http://blogs.msdn.com/b/astoriateam/archive/2012/04/11/what-happened-to-application-json-in-wcf-ds-5-0.aspx. It talks about GET but the same things applies to POST and such as well.

    So to fix your problem, modify your client to either send DataServiceVersion: 2.0 (or 1.0) if that would be correct, or even better modify it to send Content-Type: application/json;odata=verbose (which will work no matter the payload version).