Kikin
Kikin

Reputation: 11

RestClient DELETE Operation Json Error

I'm trying to make a Delete action with RestClient but I'm not able to include my Json object inside the request body. This is my code:

        var client = new RestClient(WebService);
        var request = new RestRequest(string.Format("/api/v1/{0}/{1}", controller,    action), method);

        request.AddHeader("Accept", ContentTypeApplicationJson);
        request.RequestFormat = DataFormat.Json;            
        request.AddHeader("Content-Type", ContentTypeApplicationJson);
        request.AddCookie(".ASPXAUTH", AspxAuth);
        request.AddBody(jsonObjectRequest);

        var response = client.Execute(request);

        var jsonResponse = string.Empty;
        using (var stream = new MemoryStream(response.RawBytes))
        {
            stream.Position = 0;
            var sr = new StreamReader(stream);
            jsonResponse = sr.ReadToEnd();
        }

        var jObjectDeserialize = GetJObjectDeserialize(jsonResponse);

In my example ContentTypeApplicationJson = application/json and jsonObject is a object which contain:

{ "BasketItemReferenceGuid": "sample string 1", "BasketReferenceGuid": "sample string 2" }

This is my request in Fiddler

DELETE http://local.webapi.com/api/v1/BasketV3Products/ HTTP/1.1
Accept: application/json
Content-Type: application/json
User-Agent: RestSharp 102.4.0.0
Host: local.webapi.com
Cookie: .ASPXAUTH=E6D216034E2CB1A22466A501392B1E2E46601E345B8A0E7743D76CF2270ACFC8ED3C9F1F2F477C4499267222A250E4490291381EE68FE719E094EF1ACDD619B4D792341988F80CB67E8B5037D8ACF9FBABB74DE1E75A530AB432D85722D647771C6C576F8E810257CE9E60117DCEEFBD949EAD9E64C84898BDC5D691F957CE6266CF5652C693B86ED1D55907AAC5DC68
Content-Length: 0
Accept-Encoding: gzip, deflate

Thanks

Upvotes: 1

Views: 1005

Answers (1)

Davin Tryon
Davin Tryon

Reputation: 67296

It looks like even though the HTTP spec doesn't explicitly say that a DELETE should not have a body, many web servers (or web proxies) will ignore it.

Semantically, when using DELETE, the uri should identify the resource to DELETE. This is kind of like using GET. Again, with GET, the spec does not forbid a body, but not many web servers (or web proxies) actually support a body on GET requests.

For more info, see this SO post.

So, the better way to implement your DELETE would be to place BasketItemReferenceGuid and BasketReferenceGuid in the uri template.

Upvotes: 1

Related Questions