【问题标题】:FOSRestBundle is not considering the format in my URLFOSRestBundle 没有考虑我的 URL 中的格式
【发布时间】:2016-05-07 21:19:22
【问题描述】:

我想考虑我的 URL 中的格式扩展,以便它为 _format 参数提供最高优先级。我的配置如下:

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 请求如下:

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

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

这会产生如下异常:

{"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

有趣的部分在哪里:

"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("/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)

【问题讨论】:

    标签: symfony fosrestbundle symfony-2.8 jms-serializer


    【解决方案1】:

    对于请求,Content-Type 标头定义了请求正文的媒体类型。您 POST 到的 URL 无效,请求正文的默认媒体类型为 text/plain,因此此行为是正确的。

    例如,您可以这样做:

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

     

    200 OK
    

    那么:

    GET /foo.json
    

     

    200 OK
    Content-Type: text/json; charset=utf-8
    
    {"this":"that"}
    

    【讨论】:

      【解决方案2】:

      对我来说,问题是我正在发送序列化的表单数据,我用数组解决了:

      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();
                          }
                      });
      

      希望这对你有用;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-14
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        相关资源
        最近更新 更多