0

我想考虑我的 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)
4

2 回答 2

2

对于请求,Content-Type标头定义了请求正文的媒体类型。您发布到的 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"}
于 2016-06-22T08:38:02.927 回答
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();
                    }
                });

希望这对你有用;)

于 2017-09-01T23:41:13.893 回答