Search code examples
c#wcfvisual-studiowcf-data-servicesodata

OData & WCF : Unable to perform POST,PUT & DELETE


I have configured & built WCF and oData with Visual Studio 2012 using this tutorial.

So I have used Firefox REST Client plugin to test whether its working fine or not.

Here is my base URL which is dealing with json, 192.168.1.4/TestdataService/TestDataService.svc/User?$format=json

I can do GET operation successfully. However I'm unable to perform POST,PUT & DELETE operations.

POST operation says

Status Code: 415 Unsupported Media Type

Its responds body is

 {
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "Unsupported media type requested."
    }
  }
}

PUT & DELETE operations says

Status Code: 405 Method

Not Allowed Allow: GET, POST

Its responds body is

{
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "The URI 'http://192.168.1.4/TestdataService/TestDataService.svc/User?$format=json' is not valid for DELETE operation. The URI must refer to a single resource from an entity set or from a property referring to a set of resources."
    }
  }
}

I'm attaching the screenshots below. Any help on this is appreciated.

Thanks.

GET enter image description here

POST enter image description here

enter image description here

enter image description here

PUT enter image description here

enter image description here

enter image description here


Solution

  • I think you have two unrelated problems here. The PUT and DELETE verbs must refer to a specific entity, not an entity set. When you are PUT-ting to TestDataService.svc/User, that's like trying to update the entire entity set, which isn't allowed. Instead, you need to specify which entity you want to update (for example, by PUT-ting to TestDataService.svc/User(0), or however the keys look for your entity set).

    The POST problem is different. In the POST case you should be targeting the entity set instead of an invidivual entity. The error you're getting is related to the Content-Type or Accept header of the request. For now (to simpify debugging), I would leave off the $format=json parameter in the URI, since that overrides the Accept header. Try including the following header values in the POST request:

    Accept: application/json
    Content-Type: application/json
    DataServiceVersion: 3.0;
    MinDataServiceVersion: 3.0;
    MaxDataServiceVersion: 3.0;
    

    And let us know if you're still getting an error.

    Again, this POST request should be going to TestDataService.svc/User, like you had originally. PUT and DELETE should point to individual entities (e.g., TestDataService.svc/User(0))

    Also, as qujck points out in a comment above, in your screenshots, the request body for PUT and POST are empty. POST means "insert this entry", so you need to provide an entry to be inserted in the body of the request. PUT means "replace/update this entry", so you need to provide data to the server in that case as well.