简体   繁体   中英

How to handle PUT/DELETE requests in Symfony2 FOSRestBundle

When I use PUT or DELETE requests on rest route generate by FOSRestBundle , it returns

Forbidden

You don't have permission to access /app_dev.php/api/resources/3 on this server. Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.

When I use GET or POST then it works just fine!

Here is my rest config:

fos_rest:
    format_listener:
        rules:
            - { prefer_extension: false, priorities: ['json','xml','html'], fallback_format: json }
    view:
        view_response_listener: true
        formats:
            xml:    true
            json:   true
        templating_formats:
            html:   true
        mime_types:
            json:   ['application/json', 'application/x-json']
    routing_loader:
        default_format: json
        include_format: false
    body_listener:  true

And my rest controller:

/**
 * @Rest\View
 */
public function putResourceAction(Request $request, $id)
{

    return array(1, 2, 3);
}

It throws 403 only when I use putResourceAction or deleteResourceAction .

Thanks for any help!

If anyone is experiencing this kind of problem with PUT, DELETE or other methods unavailable, try adding this to .htaccess:

<RequireAny>
    Require method DELETE GET POST PUT OPTIONS
</RequireAny>

<Limit GET POST PUT DELETE HEAD OPTIONS>
    Order allow,deny
    Allow from all
</Limit>
<LimitExcept GET POST PUT DELETE HEAD OPTIONS>
    Order deny,allow
    Deny from all
</LimitExcept>

Other way described here to allow only specified methods

RewriteCond %{REQUEST_METHOD} !^(DELETE|GET|HEAD|OPTIONS|POST|PROPFIND|PUT) [NC]
RewriteRule .* - [F,L]

Please, check allowed methods in your web server configuration.

Information for apache

For Nginx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM