We want to open a DELETE
endpoint that allows calls to all positive integers, but not id 1 (aka element 1 cannot be deleted)
Usually to open an endpoint that allows positive integers I configure the route like this
delete_elements:
path: /elements/{id}
methods: ["DELETE"]
controller: app.elements_delete
requirements:
id: '\d+'
For this case I tried to change the regex to one that does not allow number 1 either
delete_elements:
path: /elements/{id}
methods: ["DELETE"]
controller: app.elements_delete
requirements:
id: /^(?!(?:1)$)\d+/
But when I modify the requirement and I call the endpoint the response is that the endpoint does not exist.
"No route found for "DELETE /elements/59": Method Not Allowed (Allow: GET, PUT)"
What is wrong with that regex? What would be the right way to forbid certain values?
My Answer:
DELETE]_ requirements:_ id: \d+This route configuration allows calls to any positive integer for the {id} parameter. However, to exclude id 1 from being deleted, we can use negative lookaround in the regular expression pattern. Here's how the updated route configuration would look like:_delete_elements:_ path: /elements/{id}_ methods: [DELETE]_ requirements:_ id: (?
Rate this post
3 of 5 based on 3770 votesComments