如何调试需要接受headers的路由?
How to debug routes that require accept headers?
我在 API 版本控制上关注 FOSRest documentation。
但是我不清楚 headers 如何使用 media_type
将其连接到特定控制器。
现在我正在发送 headers: Accept: application/json;version=1.0
扩展名 Chrome Postman 到 /api/user/status
但是我收到 No route found for "POST /api/user/status"
错误
这是我的配置:
routing.yml:
type: rest
prefix: /api
resource: Acme\Bundle\SomeBundle\Controller\DefaultController
DefaultController.php:
use FOS\RestBundle\Controller\Annotations\Version;
/**
* @Version("1.0")
* @RouteResource("User", pluralize=false)
*/
class User
...
public function postStatusAction()
config.yml
fos_rest:
versioning:
enabled: true
resolvers:
query: false
custom_header: false
media_type:
enabled: true
regex: '/(v|version)=(?P<version>[0-9\.]+)/'
routing_loader:
default_format: json
view:
mime_types:
json: ['application/json;version=1.0']
format_listener:
enabled: true
控制台debug:routerpost_user_status
| Route Name | post_user_status |
| Path | /api/user/status.{_format} |
| Path Regex | #^/api/user/status(?:\.(?P<_format>json|xml|html))?$#s |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | POST |
| Requirements | _format: json|xml|html |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: AcmeSomeBundle:Default:postStatus |
| | _format: json |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
我也试过routing.ymlcondition: "request.attributes.get('version') == '1.0'"
都没有用
我错过了什么?
部分感谢@Mohammad 在评论中为我指明了正确的方向,我最终找到了答案。
再次查看 FOSRest versioning documentation 时,应按字面意思理解说明。
要求之一
You must configure the possible mime types for all supported versions:
这意味着因为我只有:
view:
mime_types:
json: ['application/json;version=1.0']
即使在 header 中发送请求了一个不存在的 API 版本,无论如何它仍然默认为这个版本。
正确的配置是
view:
mime_types:
json: ['application/json','application/json;version=1.0']
Mohhamad 提到 media_type
header 需要 format_listener
format_listener:
enabled: true
rules:
- { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: false }
这样它就可以正确地路由到正确的 API 版本号。
如果版本号不正确或没有 API 版本 header,它最终也会路由到 404 响应。
我在 API 版本控制上关注 FOSRest documentation。
但是我不清楚 headers 如何使用 media_type
将其连接到特定控制器。
现在我正在发送 headers: Accept: application/json;version=1.0
扩展名 Chrome Postman 到 /api/user/status
但是我收到 No route found for "POST /api/user/status"
错误
这是我的配置:
routing.yml:
type: rest
prefix: /api
resource: Acme\Bundle\SomeBundle\Controller\DefaultController
DefaultController.php:
use FOS\RestBundle\Controller\Annotations\Version;
/**
* @Version("1.0")
* @RouteResource("User", pluralize=false)
*/
class User
...
public function postStatusAction()
config.yml
fos_rest:
versioning:
enabled: true
resolvers:
query: false
custom_header: false
media_type:
enabled: true
regex: '/(v|version)=(?P<version>[0-9\.]+)/'
routing_loader:
default_format: json
view:
mime_types:
json: ['application/json;version=1.0']
format_listener:
enabled: true
控制台debug:routerpost_user_status
| Route Name | post_user_status |
| Path | /api/user/status.{_format} |
| Path Regex | #^/api/user/status(?:\.(?P<_format>json|xml|html))?$#s |
| Host | ANY |
| Host Regex | |
| Scheme | ANY |
| Method | POST |
| Requirements | _format: json|xml|html |
| Class | Symfony\Component\Routing\Route |
| Defaults | _controller: AcmeSomeBundle:Default:postStatus |
| | _format: json |
| Options | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
我也试过routing.ymlcondition: "request.attributes.get('version') == '1.0'"
我错过了什么?
部分感谢@Mohammad 在评论中为我指明了正确的方向,我最终找到了答案。
再次查看 FOSRest versioning documentation 时,应按字面意思理解说明。
要求之一
You must configure the possible mime types for all supported versions:
这意味着因为我只有:
view:
mime_types:
json: ['application/json;version=1.0']
即使在 header 中发送请求了一个不存在的 API 版本,无论如何它仍然默认为这个版本。
正确的配置是
view:
mime_types:
json: ['application/json','application/json;version=1.0']
Mohhamad 提到 media_type
header 需要 format_listener
format_listener:
enabled: true
rules:
- { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: false }
这样它就可以正确地路由到正确的 API 版本号。
如果版本号不正确或没有 API 版本 header,它最终也会路由到 404 响应。