Search code examples
jsonfosrestbundlesymfony

Returning 400 BAD REQUEST on each POST - fosrestbundle, Symfony3


I am using fosrestbundle on Symfony3 to create a server side web-api.

fos_rest configuration of app/config/config.yml

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener:
            enabled: true
            rules:
                - { path: ^/api, priorities: [ json ], fallback_format: json, prefer_extension: true }
    view:
        view_response_listener: 'force'
        formats:
            xml: true
            json : true
        templating_formats:
            html: true
        force_redirects:
            html: true
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
    routing_loader:
        default_format: json

Whenever I am posting with Content-Type: x-www-form-urlencoded the Controller get hit. Each time I use application/json the response is 400 BAD REQUEST. But I need it to be Content-Type: application/json. Please tell me what should I change in fos_rest config so that it can accept application/json. Thanks in advance.

N.B. I tried Body Listener by the book. But no luck. Version change may be the problem. I don't know.


Solution

  • As @xabbuh mentioned, you are trying with wrong data format while requesting.

    enter image description here

    Instead of above, your request should look like this.

    enter image description here