7

I have a route with this route

/**
 * @Method({"DELETE"})
 * @Route("/secure/users")
 */

When I try to do a cUrl

<html>
    <head>
        <meta charset="UTF-8" />
        <title>An Error Occurred: Method Not Allowed</title>
    </head>
    <body>
        <h1>Oops! An Error Occurred</h1>
        <h2>The server returned a "405 Method Not Allowed".</h2>

        <div>
            Something is broken. Please let us know what you were doing when this error occurred.
            We will fix it as soon as possible. Sorry for any inconvenience caused.
        </div>
    </body>
</html>

I tried to enable also

Request::enableHttpMethodParameterOverride();

in app.dev and app_dev.php, infact I can handle the PUT requests.

monkeyUser
  • 4,301
  • 7
  • 46
  • 95
  • How do you perform the request? Is the HTTP method actually DELETE (from the error message it seems that you send a request with a different HTTP method)? – xabbuh Jan 15 '16 at 20:18

1 Answers1

1

Add this parameter to your curl request :

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

or in command line

curl -X DELETE "http://localhost/secure/users"

If you are performing an XHR request using jQuery, just do

$.ajax({
    url: '/secure/users',
    type: 'DELETE',
    data: { id: resourceToDelete }
    success: function(result) {
        // Do something with the result
    }
});

And for pure javascript :

var req = new XMLHttpRequest();
req.open('DELETE', '/secure/users');
req.setRequestHeader("Content-type", "application/json");
req.send({ id: 'entityIdentifier' });

If you want access it by browser or pass query params like /secure/users?id=x, use GET :

/**
 * @Method({"GET"})
 * @Route("/secure/users")
 */

See What is the usefulness of PUT and DELETE HTTP request methods?

Community
  • 1
  • 1
chalasr
  • 12,971
  • 4
  • 40
  • 82
  • Hi, My params come form Query Parameter, I need to enable DELETE, because I'm using CURL only for test, but in reality this call comes from javascript – monkeyUser Jan 16 '16 at 13:00
  • NEVER use a GET to delete objects on the server! Otherwise you might some day wonder why sometimes objects randomly disappear (caused by browsers prefetching those URLs for example). If you don't want to use Javascript, use a form with POST. – aferber Jan 22 '17 at 09:46