Fosrestbundle: No matching accepted Response format could be determined (406 Not Acceptable)

Created on 1 Jan 2014  路  3Comments  路  Source: FriendsOfSymfony/FOSRestBundle

@lsmith77 hi Lukas :) symfonycon and hacking with you convinced me to learn REST, so here I am.. haveing little issues :P

I've started symfony-rest-edition project and created one bundle -> Acme\SecurityBundle, with User entity which extends the FOSUserBundle User model.

I've copy-pasted the code from AcmeDemoBundle and started refactoring it to deal with my User entity, instead of Note model.

I can navigate to /users and see users list, but I'm getting No matching accepted Response format could be determined (406 Not Acceptable) error from the AJAX call to _wdt profiler.

This is my routing.yml:

##
#   Homepage Redirect
##
_welcome:
    pattern:  /
    defaults:
        _controller:    FrameworkBundle:Redirect:redirect
        route:          get_users
        permanent:      true

##
#   SecurityBundle API
##
AcmeSecurityBundle_User:
    resource: "@AcmeSecurityBundle/Controller/UserController.php"
    type:     rest
    prefix:   /api

##
#   API Documentation
##
NelmioApiDocBundle:
    resource: "@NelmioApiDocBundle/Resources/config/routing.yml"
    prefix:   /api/doc

This is my routing_dev.yml:

_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml

This is my fos_rest config:

fos_rest:
    disable_csrf_role: ROLE_API
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
            xml:  true
            json: true
        templating_formats:
            html: true
    format_listener:
        rules:
            - { path: ^/api/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_listener: true

Most helpful comment

You need to add a rule to handle the toolbar otherwise you end up with no request format being matched.

For example something like the following:

            - { path: ^/api/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
            - { path: '^/', priorities: [ 'html', '*/*'], fallback_format: html, prefer_extension: true }

All 3 comments

You need to add a rule to handle the toolbar otherwise you end up with no request format being matched.

For example something like the following:

            - { path: ^/api/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
            - { path: '^/', priorities: [ 'html', '*/*'], fallback_format: html, prefer_extension: true }

@lsmith77 thanks, it works now :)

I opened a ticket to automate adding a rule specifically for the profiler:
https://github.com/FriendsOfSymfony/FOSRestBundle/issues/652

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bblue picture bblue  路  7Comments

feridmovsumov picture feridmovsumov  路  7Comments

dkorsak picture dkorsak  路  5Comments

JeroenDeDauw picture JeroenDeDauw  路  6Comments

kbkk picture kbkk  路  7Comments