Вопрос

I need to support different response formats like JSON and HTML, so far this was good with JSON response formats, now I tried to support HTML response from my APIs and facing issues.

I need to return JSON response from some of the APIs and HTML response from some APIs, but all the APIs(methods) present in same class.

If I add both JSON and HTML in supported formats list then all APIs are returning HTML response only, not sure how to manage this situation.

$r->setSupportedFormats('JsonFormat', 'HtmlFormat');

This is the code block I am using to set JSON and HTML in supported formats list, anybody please let me know how to handle this scenario.

index.php

use Luracast\Restler\Defaults;  
use Luracast\Restler\Filter\RateLimit;  
use Luracast\Restler\Format\UploadFormat;  
use Luracast\Restler\Format\HtmlFormat;  
use Luracast\Restler\Restler;  

require_once 'vendor/restler.php';  
require_once('config.php');    

Defaults::$throttle = 20;  
Defaults::$cacheDirectory = PLAY_API_CACHE_DIR;

// Setup restler  
$r = new Restler();  
$r->addAPIClass('test');  
$r->addAPIClass('Resources');   
$r->addFilterClass('RateLimit');  
$r->setSupportedFormats('JsonFormat', 'UploadFormat', 'HtmlFormat');  
$r->handle();  

test.php

require_once 'BaseHandler.php';

class test extends BaseHandler {

    // Initialize API class attributes
    public function __construct() {
        parent::__construct();
    }

    /**
    * Request the breakdown,by category, of a user's synced data.
    * 
    * @param string $auth_token SSO Authentication Token
    *
    * @url GET getStorageUsage
    */
    public function getStorageUsage($auth_token = '') {

        // Required parameters checkup
        if (!$auth_token && isset($_SESSION['play_auth_token'])) $auth_token = $_SESSION['play_auth_token'];
        if (!$auth_token  )    return PlayErrorCodes::throwParameterMissingError();

        // Get a breakdown,by category, of a user's synced data using Sync API call
        return $this->callAPI('sync', 'getStorageUsage', array('auth_token' => $auth_token));    
    }

    /**
    * Requests the full HTML document representing a users data graphically
    * 
    * @param string $auth_token SSO Authentication Token
    * @param string $client Name of the client requesting the widget.  android is the only acceptable and default value.
    * @param string $path Path of the resource to display.  Defaults to '/'
    *
    * @url GET getWidget
    * @view getWidgetView
    */
    public function getWidget($auth_token = '',$client = '',$path = '') {
        // Required parameters checkup
        if (!$auth_token && isset($_SESSION['play_auth_token'])) $auth_token = $_SESSION['play_auth_token'];
        if (!$auth_token)    return PlayErrorCodes::throwParameterMissingError();

        // Get the full HTML document representing a users data graphically using Sync API call
        $this->resDecodeFlag = false;
        return $this->callAPI('sync', 'getWidget', array('auth_token' => $auth_token, 'client' => $client, 'path' => $path)); 
    }

}

Here I need to return JSON response for 'getStorageUsage' and HTML response for 'getWidget'.

References I have gone through are:
http://restler3.luracast.com/examples/_013_html/readme.html
https://github.com/Luracast/Restler/tree/master/public/examples/_013_html#html-format

Thanks in advance... Siva.

Это было полезно?

Решение

As @Luceos rightly pointed out Browser sends accept headers that always prefer HTML

To overcome this situation you can always request these resources with the respective extension

  • getStorageUsage.json
  • getWidget.html

If a specific resource needs to be always in a specific format you can do the following

$r = new Restler();
$r->addAPIClass('MyApiClass');
$r->setSupportedFormats('JsonFormat', 'UploadFormat');
$r->setOverridingFormats('HtmlFormat');
$r->handle();

Then on your getWidget method add @format comment to specify HtmlFormat as shown below

/**
* Requests the full HTML document representing a users data graphically
* 
* @param string $auth_token SSO Authentication Token
* @param string $client Name of the client requesting the widget.  android is the only acceptable and default value.
* @param string $path Path of the resource to display.  Defaults to '/'
*
* @url GET getWidget
* @view getWidgetView
* @format HtmlFormat
*/
public function getWidget($auth_token = '', $client = '', $path = '') {
    //your logic comes here
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top