Correct way to use Accept header in REST api built with Symfony2 and FOSRestBundle
I am trying to get my webservices to return the correct format when sent with an Accept header. For example, if I submit "Accept: application / xml" it should return in xml format, and if I submit "Accept: application / json" it should return in json format.
This works great if I add the extension. http://example.com/api/users.json returns json and http://example.com/api/users.xml returns XML, But if I don't add extensions it always returns json, completely ignoring the Accept header.
How can I configure it to return whatever is specified in the Accept header if there is no extension?
My config file:
fos_rest:
format_listener: true
routing_loader:
default_format: json
view:
view_response_listener: 'force'
formats:
json: true
xml: true
templating_formats:
html: true
serializer:
serialize_null: true
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
Also tried the following:
format_listener:
rules:
- prefer_extension: false
As a result, an error occurred:
Cannot find template "WhateverBundle: Users: getUsers.html.twig"
source to share
I was able to do what I wanted:
fos_rest:
format_listener:
rules:
- priorities: [ json, xml, html ]
- prefer_extension: false
routing_loader:
default_format: json
view:
view_response_listener: 'force'
formats:
json: true
xml: true
templating_formats:
html: true
serializer:
serialize_null: true
For some reason it is not enough to have a prefer_extension configuration in format_listener.
source to share