2015-06-12 107 views
3

最近我升級我的休息服務器ZF2 Apigility,其內容協商設置如下,如何使ZF2 Apigilty接受客戶端的請求沒有在頭Accept,接受設置

'zf-content-negotiation' => array(
    'controllers' => array(
     'CloudSchoolBusFileApi\\V1\\Rest\\FileReceiver\\Controller' => 'Json', 
    ), 
    'accept_whitelist' => array(
     'CloudSchoolBusFileApi\\V1\\Rest\\FileReceiver\\Controller' => array(
      0 => 'application/vnd.cloud-school-bus-file-api.v1+json', 
      1 => 'application/json', 
     ), 
    ), 
    'content_type_whitelist' => array(
     'CloudSchoolBusFileApi\\V1\\Rest\\FileReceiver\\Controller' => array(
      0 => 'application/vnd.cloud-school-bus-file-api.v1+json', 
      1 => 'application/json', 
      2 => 'multipart/form-data', 
     ), 
    ), 

的問題是,我的客戶(mobie應用程序)已經部署,並且它們在http標題中發送沒有接受字段設置的發佈請求。所以我總是得到以下來自服務器的406錯誤,

[Response] => Array 
(
    [statusCode] => 406 
    [content] => {"type":"http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html","title":"Not Acceptable","status":406,"detail":"Cannot honor Accept type specified"} 
) 

因此,任何人對如何讓服務器接受來自客戶端的報頭沒有受理該申請的想法?

回答

2

您可以編寫一個偵聽器,在其中檢查傳入請求的Accept標頭。如果沒有設置Accept標頭,則可以添加帶有默認值的Accept標頭;例如application/json

因此,像:

/** 
* Set empty accept header by default to `application/json` 
* 
* @param MvcEvent $event 
* @return void|ApiProblemResponse 
*/ 
public function onRoute(MvcEvent $event) 
{ 
    $request = $event->getRequest(); 
    $headers = $request->getHeaders(); 

    if($headers->has('Accept')){ 
     // Accept header present, nothing to do 
     return; 
    } 

    $headers->addHeaderLine('Accept', 'application/json'); 
} 

更好的當然會是更新客戶端。

+0

謝謝!我最終攻擊了zf-content-negotiation模塊,AcceptFilterListener.php,添加了上面的代碼,它工作正常! –

+0

有同樣的問題,但與soapui:我添加了標題,它的工作 – cwhisperer

相關問題