Search code examples
phprestrestler

Make JSON the default format in Resetler?


I'm going through the tutorial examples of Restler here: http://restler3.luracast.com/examples/_003_multiformat/readme.html

While it states that:

First format specified in Restler::setSupportedFormats is used as the default format when client does not specify the format.

I find it not true. As while I have:

use Luracast\Restler\Restler;
$r = new Restler();
$r->setSupportedFormats('JsonFormat', 'XmlFormat');
$r->addAPIClass('BMI', '/api/data/bmi');
$r->handle();

Which is exactly the same as that of the code given on the example page, but when I access /api/data/bmi it's giving XML rather than JSON.

I had to go to /api/data/bmi.json to get the JSON format. Ain't 'JsonFormat' the first parameter for setSupportedFormats() here? Why isn't JSON the default format? How to make it the default?


Solution

  • I guess you have tried calling the api using web browser. Browser sends an accept header where html and xml are accepted that is why xml is rendered by default. Try calling it with no accept header using curl