私は Web サービスを構築しており、json インターフェイスのみを提供するつもりです。
そのために、ユーザーが API を操作するのをできるだけ簡単にしたいと考えています。理想的な世界では、消費者は「application/json」の Content-Type ヘッダーを使用してリクエストを送信しますが、Content-Type ヘッダーに関係なく、API で json エンコードされたリクエストを受け入れるようにしたいと考えています。
私は Symfony2 と FOS_RESTBundle を使用しており、ボディ リスナーとフォーマット リスナーを、フォーマットをアプリケーション json に強制するコントローラーへのルートと共に設定しました。
Content-Type が「application/json」のリクエストを送信すると、本文が正しくデコードされます。以下の例では、その他の Content-Type ヘッダーの結果は $data の null 値です。$type は常に json です (ルートの _format のため)。
どこが間違っていますか?
// config.yaml
fos_rest:
view:
view_response_listener: false
failed_validation: HTTP_BAD_REQUEST
default_engine: php
formats:
text: false
xml: false
html: false
json: true
format_listener:
default_priorities: ['application/json']
fallback_format: json
prefer_extension: true
body_listener:
decoders:
json: fos_rest.decoder.json
と -
// Routing.yaml
_api_contact_request_create:
pattern: /api/merchant/{merchantId}/customer-contact-request
defaults: { _controller: AcmeApiBundle:CustomerContact:Create, _format: json}
type: rest
requirements:
_method: POST
ついに -
// Extract from Controller
public function CreateAction($merchantId) {
$data = $this->getRequest()->request->get('data');
$type = $this->getRequest()->getRequestFormat();
var_dump($type, $data);die;