7

I have a DTO coming from the Javascript client. When I try to send with deleteFromService the request object is empty (looks like it was just new-ed up). If I change the method to postToService the request object is populated properly.

I am using the 3.9 API. I am at a loss here.

Service:

public object Post(Order request)
{
    return _orderRepository.InsertOrder(request);
}

public object Delete(Order request)
{
    _orderRepository.DeleteOrder(request.Uuid);
    return true;
}

JS:

//Fails
serviceClient.deleteFromService("order", order, function () { }, deleteFailed);
//Works
serviceClient.postToService("order", order, function () { }, deleteFailed);

Update:

I found the issue in the ServiceStack source code. It is treating a DELETE like a GET and creating the request object instead of the body, like it does for a POST.

if (httpMethod == HttpMethods.Get || httpMethod == HttpMethods.Delete || httpMethod == HttpMethods.Options)
{
    try
    {
        return KeyValueDataContractDeserializer.Instance.Parse(queryString, operationType);
    }
}

The problem with doing this, is that the ServiceStack JS client creates the DELETE request using the same logic as the POST, by stuffing the data you send it into the body (technically the jQuery XHR data prop), meaning there is no way for the server to get the message the client is sending.

Am I reading this right? Is the JS client's delete broken?

ean5533
  • 8,884
  • 3
  • 40
  • 64
Kyeotic
  • 19,697
  • 10
  • 71
  • 128
  • 3
    There is clearly a fundamental disconnect between the ServiceStack server and the ServiceStack JS client. The JS client thinks DELETE information should go in the request payload, but the server thinks it should be in the query string. It seems to me that the JS client is wrong. – ean5533 Feb 07 '13 at 23:26
  • looks like a bug in the JS client... open an issue IMO https://github.com/ServiceStack/ServiceStack/issues?state=open – Yaur May 22 '13 at 23:51

2 Answers2

2

I rewrote the servicestack.js client adding the following line in the P.Send function before the options var initialization...

if (ajaxOptions.type === "DELETE") {
    requestUrl = requestUrl + "?" + decodeURIComponent($.param(request[webMethod]));
}
Nunser
  • 4,512
  • 8
  • 25
  • 37
1

A DELETE message should not carry a message body, meaning you are only allowed to pass parameters to the DELETE request in the url (i.e. like the ID of the object you want to delete). So, besides what ServiceStack does, you should never send a DTO in a DELETE request.

See this related question: Is an entity body allowed for an HTTP DELETE request?

Community
  • 1
  • 1
Dynalon
  • 6,577
  • 10
  • 54
  • 84
  • The question you link to does not say what you are saying. – Kyeotic Jun 01 '13 at 19:47
  • Not only read the answer marked as valid, but the others, too. The HTTP spec seems to be ambigous at that point, but quite some implementations just ignore request entities (=body) when provided with a DELETE request. The question marked as accepted also says that Microsoft ignores request entities for their ADO.NET implementation. The error you describe matches this behavior: The request DTO might carry default values because the message body (the JSON, XML or whatever) was ignored. – Dynalon Jun 01 '13 at 20:38
  • 1
    I understand the point, but I don't see how it relates to my point. Obviously the message body is being ignored, I said as much is my own question. You aren't adding new information. My point is that ServiceStack's server and client disagree on what to do; what does that have to do with Microsoft, ADO, or the HTTP spec? – Kyeotic Jun 01 '13 at 20:53
  • I am not referring to your "point", but am answering your question - this is a Q&A site. Your question, as written above and as I understand it, asks why the request object is empty. The answer to that is: It is empty, because the server choses to ignore the message body (which holds the JSON that would be required to populate the DTO). If I misunderstood your exakt question, please rephrase (and remember questions end with a ?). – Dynalon Jun 01 '13 at 21:05
  • I did end it with a "?", its the last sentence in my question: is the JS client broken, or am I reading the code incorrectly. Your answer does not address that, or ServiceStack in general. – Kyeotic Jun 01 '13 at 21:42
  • My second question, in previous comment, was what ADO or Microsoft have to do with ServiceStack, which you also don't answer (which also ended with "?"). – Kyeotic Jun 01 '13 at 21:45