Yinfang Zhuang
Yinfang Zhuang

Reputation: 451

Got bad request error for a DELETE REST web service

In my interface I defined:

[OperationContract]
[WebInvoke(Method = "DELETE",
    UriTemplate = "deleteAsset/{asset_id}")]
void deleteAsset(string asset_id);

My implementation is as follows:

public void deleteAsset(string asset_id)
{
    using (DataClassesDataContext thisContext = new 
        DataClassesDataContext(ConfigurationManager.ConnectionStrings
        ["mgatewayConnectionString"].ConnectionString))
    {
            var result =
                thisContext.spGetAssetById(Convert.ToInt32(asset_id)).FirstOrDefault();
            if (result != null)
            {
                thisContext.spDeleteAsset(Convert.ToInt32(asset_id));
                thisContext.spDeleteModuleAssetLink(Convert.ToInt32(asset_id));
            }
    }//using
}

I use RESTClient for Firefox for testing. I chose DELETE from the Method dropdown list and entered the url: localhost:57518/Service.svc/deleteAsset/127 and hit "Send" button. Then I got this error. I got the following error message:

400 Bad Request

The server encountered an error processing the request.

Upvotes: 1

Views: 1175

Answers (2)

Jonny Cundall
Jonny Cundall

Reputation: 2612

The problem is probably not in the code you've posted.

a 400 error should generally only happen if you send a malformed http request. So whatever way you're testing it, the web server is rejecting it befire it hits your method.

Upvotes: 2

Diego
Diego

Reputation: 36146

where is it happening? does it reach the "deleteAsset" methid?

Also, why are you checking if the asset exists before deleting? It seems like an unnecessary overhead. Cant you just run the delete command and if it exists it will be deleted, otherwise nothing will happen. If you need to know if something was actually deleted or not, get the affected rows from the delete command.

Upvotes: 0

Related Questions