3

I'm working on a Symfony 4.1.0 based microservice and it's got the following REST API defined in config/routes.yaml:

import:
    path: /sip/calls/
    controller: App\Controller\ApiController::import
    methods: [POST]

The problem is that a POST request to /sip/calls is causing NotFoundHttpException (No route found for "POST /sip/calls"). If I remove the trailing slash from the route path in config/routes.yaml, requests to /sip/calls pass through, but /sip/calls/ stops working.

Why does it behave like? How to make it ignore the slash or its absence?

super.t
  • 2,526
  • 7
  • 32
  • 51
  • 2
    It's a well known feature. You can search and read some of the discussions. Your route will actually work for GET but not POST. Sad fact of the matter is that as far as http is concerned, adding a trailing slash does indeed make it a different resource. You can use some regex magic to get around it or just define two routes. – Cerad Jun 07 '18 at 13:10

2 Answers2

3

As explained in: Redirecting URLs with Trailing Slashes Symfony 4.1 handle this internaly by create redirection 301. But the problem is that this redirection do not work for POST requests.

As mentioned here Why doesn't HTTP have POST redirect theoretically you could use redirection 307 or 308, but I had some issues with this in the past, so I choose simple solution with duplicated path with trailing slash.

Łukasz Jakubek
  • 995
  • 5
  • 12
2

This is intended behaviour as pointed out by @LukaszJakubek. Should you need to match the route both ways you can just assign multiple routes and it should work:

#config/routes.yaml
with_slash:
    path: test/
    controller: App\Controller\TestController::something
    methods: [POST]

without_slash:
    path: test
    controller: App\Controller\TestController::something
    methods: [POST]

Result when using the debug command:

bin/console router:match /test --method=POST



[OK] Route "without_slash" matches


+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | without_slash                                           |
| Path         | /test                                                   |
| Path Regex   | #^/test$#sD                                             |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | POST                                                    |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: App\Controller\TestController::something   |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
bin/console router:match /test/ --method=POST



[OK] Route "with_slash" matches


+--------------+---------------------------------------------------------+
| Property     | Value                                                   |
+--------------+---------------------------------------------------------+
| Route Name   | with_slash                                              |
| Path         | /test/                                                  |
| Path Regex   | #^/test/$#sD                                            |
| Host         | ANY                                                     |
| Host Regex   |                                                         |
| Scheme       | ANY                                                     |
| Method       | POST                                                    |
| Requirements | NO CUSTOM                                               |
| Class        | Symfony\Component\Routing\Route                         |
| Defaults     | _controller: App\Controller\TestController::something   |
| Options      | compiler_class: Symfony\Component\Routing\RouteCompiler |
+--------------+---------------------------------------------------------+
dbrumann
  • 16,803
  • 2
  • 42
  • 58
  • 1
    Awesome. This seems to work with annotations as well. Thanks. – pusle Jan 21 '19 at 20:38
  • this doesn't work for me when prefix is used - i.e. route defined in bundle and prefixed in application using that bundle – meridius Mar 10 '21 at 14:19
  • @meridius It depends on how the bundle provides the routes. Theoretically you should be able to overwrite them. Maybe you can open a new question with your specific problem, i.e. which bundle is causing your problems and your (& the bundle's) routing configuration? – dbrumann Mar 10 '21 at 14:23