In the example, would any value match the longer route?

Question

In the example, would any value match the longer route?

Answer

Yes, but with repercussions.
In the first example, it is mention that the /monsters route can receive a parameter :name, in that case, the possible responses from that endpoint will be related to names that might be already existing on that server, if we passed the last example: /monsters/123 the request will match the path with the /:name route parameter, but chances are 123 is not a name and thus the response will be a result not found or in case that the API sends positive responses in those cases, it will have something along the lines of: {results: 0}.

The same can be said about /expressions/:id. Any get request that has a value after the expressions path, ie.: /expressions/smiley will successfully go through and match, but again, id usually relates to a unique identifier that tends to be a serial number so "smiley" will either not return results or be caught and returned as a 404 not found error.

So even though any value passed as a parameter in the correct order of the route will go through in the request, there are ways we as developers can make it more obvious that the request has received a wrong value. We can do so in the callback function by checking the parameter value to check if it is the type required before we proceed with any other actions, and maybe return a message to the user so they can know what they need.

6 Likes