Sylius / ShopApiPlugin

Shop API for Sylius.
https://sylius.com
129 stars 89 forks source link

Getting "Format 'html' not supported, handler must be implemented" on GET requests #721

Open medteck opened 2 years ago

medteck commented 2 years ago

When I try to retrieve data with A GET request (ex: /shop-api/products/by-code/abc), I'm getting this response from the API :

{
    "code": 401,
    "message": "Invalid credentials."
}

Here's my configuration in config/packages/_sylius_shop_api.yaml :

imports:
    - { resource: "@SyliusShopApiPlugin/Resources/config/app/config.yml" }
    - { resource: "@SyliusShopApiPlugin/Resources/config/app/sylius_mailer.yml" }

# config/packages/fos_rest.yaml

fos_rest:
    view:
        formats:
            json: true
    format_listener:
        rules:
            - { path: '^/shop-api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
            - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
            - { path: '^/', stop: true }

As you can see, the file is as described in the README of the package.

I tried everything I saw on Stackoverflow (related to api_platform) and none of those solutions worked.

medteck commented 2 years ago

To make it work, I had to add a line in config/packages/fos_rest.yaml :

fos_rest:
    exception: true
    view:
        formats:
            json: true
            xml:  true
        empty_content: 204
    format_listener:
        rules:
            - { path: '^/shop-api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true } # <------ Add this line
            - { path: '^/api/v1/.*', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
            - { path: '^/', stop: true }
mamazu commented 2 years ago

Thanks for reporting this issue. Did you figure out the error? It looks like a 401 which doesn't have to do anything with the fos output configuration or the format 'html' not supported.

But nevertheless the /shop-api/products/by-code route should not be protected by any authorization what so ever. This might be because of a missing setting in the security.yaml file.

Since you mentioned API platform. If you are starting a new project with Sylius and an API it is recommended to use the api platform integration of the project instead of this package.