2
votes

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?

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

2 Answers

2
votes

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.

1
votes

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 |
+--------------+---------------------------------------------------------+