Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack DELETE request is default object, POST works fine

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?

like image 472
Kyeotic Avatar asked Feb 07 '13 22:02

Kyeotic


2 Answers

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]));
}
like image 101
Guillermo Londono Avatar answered Nov 20 '22 04:11

Guillermo Londono


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?

like image 1
Dynalon Avatar answered Nov 20 '22 03:11

Dynalon