Apify implements content negotiation by parsing the URI and the Accept header.
Content negotiation is an HTTP feature that allows an HTTP server to serve different media types for the same URL, according to which media types are requested by the HTTP client. Content negotiation is more likely to be used by custom clients, such as an Ajax request that requires a JSON response.
Apify implements content negotiation by parsing the URI and the Accept header. The problem with libraries that only parse the Accept header is that some client-side platforms have limitations in their support of HTTP (specially web browsers). Apify overcomes those limitations by parsing the query string, extension and Accept header.
Query String
If URL Rewriting is disabled, specify your response format in the query string. This means a format=xml or format=json parameter for XML or JSON, respectively, which will override the Accept header if there is one.
/?method=users&format=json /?method=users&format=xml
Extension
If URL Rewriting is enabled, append a format extension to the end of the URL path (.html, .json, .xml or .rss).
/users.json /users.xml
Accept Header
If RESTful Mapping is enabled, send a standard Accept header in your request (text/html, application/xml or application/json).
Accept: application/xml
Note: Extension and query string overwrites the Accept header.
Acceptable Formats
Action methods can use the $request->acceptContentTypes() method to specify certain media types which are acceptable for the response.
class UsersController extends Controller { /** * Route /users.json * Route /users.xml * * @param Request $request * @return Response */ public function indexAction($request) { $request->acceptContentTypes(array('json', 'xml')); $response = new Response(); $response->users = array('paul', 'adrian', 'adam'); return $response; } }
You can also make JSON the default format:
class UsersController extends Controller { /** * Route /users * Route /users.json * * @param Request $request * @return Response */ public function indexAction($request) { $request->setContentType('json'); $response = new Response(); $response->users = array('paul', 'adrian', 'adam'); return $response; } }
Comments
Use this form to add corrections, additions and suggestions about the documentation on this page. If you encounter any problems, please use the GitHub issue tracker.