douwen9540 2013-12-09 10:19
浏览 19
已采纳

对Restler 3 API的HTML响应支持

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.

  • 写回答

1条回答 默认 最新

  • doulouxun6756 2013-12-11 03:48
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算