Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

405 Method Not Allowed in Symfony 3

Tags:

rest

php

symfony

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.

like image 940
monkeyUser Avatar asked Jan 15 '16 19:01

monkeyUser


1 Answers

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?

like image 109
chalasr Avatar answered Sep 29 '22 03:09

chalasr