2016-05-07 5 views
0

_formatパラメータの優先順位が最も高くなるように、URLのフォーマット拡張子を検討します。FOSRestBundleがURLのフォーマットを考慮していません

fos_rest: 
    param_fetcher_listener: true 
    view: 
     view_response_listener: 'force' 
     formats: 
      json: true 
      xml: true 
    routing_loader: 
     default_format: json 
    format_listener: 
     enabled: true 
     rules: 
      - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true } 
    serializer: 
     serialize_null: true 
    body_converter: 
     enabled: true 

と私のHTTPリクエストには、以下のいずれかです:私の構成は、次のいずれかの興味深い部分がどこにあるか

{"error":{"code":415,"message":"Unsupported Media Type","exception":[{"message":"The format \"txt\" is not supported for deserialization.","class":"Symfony\\Component\\HttpKernel\\Exception\\UnsupportedMediaTypeHttpException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"\/Users\/Matteo\/Documents\/belka\/auth\/vendor\/friendsofsymfony\/rest-bundle\/FOS\/RestBundle\/Request\/AbstractRequestBodyParamConverter.php","line":121,"args":[]},{"namespace":"FOS\\RestBundle\\Request","short_class":"AbstractRequestBodyParamConverter","class":"FOS\\RestBundle\\Request\\AbstractRequestBodyParamConverter","type":"->","function":"execute","file":"\/Users\/Matteo\/Documents\/belka\/auth\/vendor\/friendsofsymfony\/rest-bundle\/FOS\/RestBundle\/Request\/RequestBodyParamConverter.php 

POST /app_dev_local.php/api/users/admin/globaltoken.json HTTP/1.1 
Host: localhost:8000 
Cache-Control: no-cache 

{ 
    "password": "<a password>" 
} 

が、これは、このような例外を生成します。

"Unsupported Media Type","exception":[{"message":"The format \"txt\" 

このようにHTTPリクエストを変更しようとしました:

POST /app_dev_local.php/api/users/admin/globaltoken.json HTTP/1.1 
Host: localhost:8000 
Content-Type: application/json 
Cache-Control: no-cache 

{ 
    "password": "<a password>" 
} 

それが動作します!私の拡張は全く無視されます。私の設定に何か問題がありますか? JMSSerializerが誤って設定されていますか?注釈は次のとおりです。

/** 
* @View() 
* 
* @Route("https://stackoverflow.com/users/{username}/globaltoken", requirements={"user"="\w+"}) 
* @ParamConverter(
*  "userBody", 
*  class="Belka\AuthBundle\Entity\User", 
*  converter="fos_rest.request_body", 
*  options={"deserializationContext"={"groups"={"personal"}}} 
*) 
*/ 
public function postAction($username, User $userBody) 

答えて

0

リクエストでは、Content-Typeヘッダーが要求本体のメディアタイプを定義します。 POSTしたURLは効果がなく、要求本体のデフォルトのメディアタイプはtext/plainなので、この動作は正しいです。

あなたは、例えば、この操作を行うことができます:次に

PUT /foo.json 
Content-Type: application/x-www-form-urlencoded 

this=that 

200 OK 

GET /foo.json 

0

私にとっての問題は、私はシリアル化されたフォームデータを送信したことだったと私は配列で解決:

var dataj = { 
        name: (tjq('input[name="appbundle_contact[name]"]').val()), 
        description: (tjq('input[name="appbundle_contact[description]"]').val()), 
        email: (tjq('input[name="appbundle_contact[email]"]').val()), 
        country: (tjq('input[name="appbundle_contact[country]"]').val()), 
        phone: (tjq('input[name="appbundle_contact[phone]"]').val()), 
       }; 
       tjq.ajax({ 
        type  : tjq('#contactForm').attr('method'), 
        url   : tjq('#contactForm').attr('action'), 
        jsonp: "response", 
        dataType: 'json', 
        contentType: "application/json; charset=utf-8", 
        data: JSON.stringify(dataj), 
        success  : function(data, status, object) { 
         tjq('.alert-success').show(); 
         tjq('#contactForm').hide(); 
        }, 
        error: function(data, status, object){ 
         console.log(data.message); 
         tjq('.alert-error').show(); 
         tjq('#contactForm').hide(); 
        } 
       }); 

希望これはあなたのために働く;)

関連する問題