Apigility - 指定的内容类型无效

时间:2015-05-22 13:46:31

标签: php configuration zend-framework2 apigility

我正在尝试将登录凭据发布到apigility登录API:

    $response = ClientStatic::post(
        'http://www.example.com/api/login',
        array('email' => 'test@eample.com','password' => 'test'),
        array('Accept' => 'application/json')
    );

这将返回错误:

  

不能遵守指定的接受类型

据我所知,application/json已在配置中列入白名单:

有什么想法吗?

我的Apigility配置:

<?php
return array(
    'controllers' => array(
        'factories' => array(
            'Api\\V1\\Rpc\\Login\\Controller' => 'Api\\V1\\Rpc\\Login\\LoginControllerFactory',
        ),
    ),
    'router' => array(
        'routes' => array(
            'api.rpc.login' => array(
                'type' => 'Segment',
                'options' => array(
                    'route' => '/api/login',
                    'defaults' => array(
                        'controller' => 'Api\\V1\\Rpc\\Login\\Controller',
                        'action' => 'login',
                    ),
                ),
            ),
        ),
    ),
    'zf-versioning' => array(
        'uri' => array(
            0 => 'api.rpc.login',
        ),
    ),
    'zf-rpc' => array(
        'Api\\V1\\Rpc\\Login\\Controller' => array(
            'service_name' => 'Login',
            'http_methods' => array(
                0 => 'POST',
                1 => 'GET',
            ),
            'route_name' => 'api.rpc.login',
        ),
    ),
    'zf-content-negotiation' => array(
        'controllers' => array(
            'Api\\V1\\Rpc\\Login\\Controller' => 'Json',
        ),
        'accept_whitelist' => array(
            'Api\\V1\\Rpc\\Login\\Controller' => array(
                0 => 'application/vnd.api.v1+json',
                1 => 'application/json',
                2 => 'application/*+json',
            ),
        ),
        'content_type_whitelist' => array(
            'Api\\V1\\Rpc\\Login\\Controller' => array(
                0 => 'application/vnd.api.v1+json',
                1 => 'application/json',
            ),
        ),
    ),
    'zf-content-validation' => array(
        'Api\\V1\\Rpc\\Login\\Controller' => array(
            'input_filter' => 'Api\\V1\\Rpc\\Login\\Validator',
        ),
    ),
    'input_filter_specs' => array(
        'Api\\V1\\Rpc\\Login\\Validator' => array(
            0 => array(
                'required' => true,
                'validators' => array(
                    0 => array(
                        'name' => 'Zend\\Validator\\EmailAddress',
                        'options' => array(
                            'useDomainCheck' => true,
                        ),
                    ),
                ),
                'filters' => array(),
                'name' => 'email',
                'description' => 'Email address',
                'error_message' => 'Email address error',
            ),
            1 => array(
                'required' => true,
                'validators' => array(),
                'filters' => array(),
                'name' => 'password',
                'description' => 'Password required',
                'error_message' => 'Password error',
            ),
        ),
    ),
    'zf-mvc-auth' => array(
        'authorization' => array(
            'Api\\V1\\Rpc\\Login\\Controller' => array(
                'actions' => array(
                    'Login' => array(
                        'GET' => false,
                        'POST' => false,
                        'PUT' => false,
                        'PATCH' => false,
                        'DELETE' => false,
                    ),
                ),
            ),
        ),
    ),
);

3 个答案:

答案 0 :(得分:1)

您确实在controllers配置中设置了zf-content-negotiation密钥,并指向Json选择器。但是没有配置此选择器。这可能是您问题的一部分。您还可以阅读此in the zfcampus/zf-content-negotiation documentation。你应该添加如下内容:

'selectors'   => array(
    'Json' => array(
        'ZF\ContentNegotiation\JsonModel' => array(
            'application/json',
            'application/*+json',
        ),
    ),
),

如果这不能解决您的问题,请添加评论...

答案 1 :(得分:1)

您可以在selector

下的管理界面上添加新的[main menu] -> Content Negotiation -> [button] New selector

enter image description here

并在那里添加/删除/编辑选择器,例如向他们添加ViewModel

[side menu] -> Foo API -> Bar service -> Content Negotiation中提供了服务的内容协商设置。可以从内容列表中选择服务的Content Negotiation Selector,并且可以编辑媒体类型

enter image description here

(删除似乎是错误的并且无法正常工作 - 但无论如何也可以在密钥zf-content-negotiation中的module.config.php中完成。)

答案 2 :(得分:0)

这种情况下的解决方案是运行composer.phar更新来解决问题。