How to go beyond PUT and DELETE limitations on RESTful Scenarios
- December 21st, 2009
- Posted in Tech Recipes . WCF
- By Germán Medina
- Write comment
One of the biggest problems you will have while implementing RESTful services is that most of today’s browsers and firewalls will not allow PUT and DELETE requests. An easy way to fix this is to add a custom HTTP header to your post requests and send the real method in there. Since Google is using “X-HTTP-Method-Override” it looks like a smart choice to follow that pattern and use it too.
This simple jquery code shows how to do it in the client side:
$.ajax({
type: "POST",
url: serviceURL,
data: "data",
success: function(data, textStatus) { alert("success"); },
error: function(xhr, status, error) { alert("error"); },
beforeSend: function(xhr) { xhr.setRequestHeader("X-HTTP-Method-Override", "DELETE"); }
});
This C# code shows how to use the custom header in a WCF server:
public String PostProxy(String data)
{
switch (HttpContext.Current.Request.Headers["X-HTTP-Method-Override"])
{
case "PUT": return Add(data);
case "DELETE": return Delete(data);
default: return Update(data);
}
}
No comments yet.