From 115dbc721d77509274e7a1bacf0239ada565b005 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 11 Dec 2012 22:36:46 +0000 Subject: [PATCH] API: Specify the response format using a GET parameter --- lib/api.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/api.php b/lib/api.php index d11c3799d9a..cc1d9fccaff 100644 --- a/lib/api.php +++ b/lib/api.php @@ -66,10 +66,8 @@ class OC_API { $name = str_replace(array('/', '{', '}'), '_', $name); if(!isset(self::$actions[$name])){ OC::getRouter()->useCollection('ocs'); - OC::getRouter()->create($name, $url.'.{_format}') + OC::getRouter()->create($name, $url) ->method($method) - ->defaults(array('_format' => 'xml') + $defaults) - ->requirements(array('_format' => 'xml|json') + $requirements) ->action('OC_API', 'call'); self::$actions[$name] = array(); } @@ -106,11 +104,9 @@ class OC_API { // Merge the responses $response = self::mergeResponses($responses); // Send the response - if(isset($parameters['_format'])){ - self::respond($response, $parameters['_format']); - } else { - self::respond($response); - } + $formats = array('json', 'xml'); + $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml'; + self::respond($response, $format); // logout the user to be stateless OC_User::logout(); } @@ -218,7 +214,7 @@ class OC_API { * @param int|array $response the response * @param string $format the format xml|json */ - private static function respond($response, $format='json'){ + private static function respond($response, $format='xml'){ if ($format == 'json') { OC_JSON::encodedPrint($response); } else if ($format == 'xml') { -- 2.39.5