Search code examples
restrestful-url

RESTful URI trailing slash or no trailing slash


Is there an authoritative position I can cite when it comes to a trailing slash on a Restful URI? One from Roy Fielding would be great. The web has authoritative opinions both ways. The two positions are: The trailing slash indicates a resource and not having does not. The other argument is that the trailing slash has no semantic value. Which is it? Example:

  @GetMapping(path = "/users/")
  public List<User> getUsers() {
   ....
  }

  @GetMapping(path = "/users/{id}")
  public User getUser(@PathVariable String type)  {
   .....
  }

  @PutMapping(path = "/users/")
  public User updateUser(@RequestBody User user) {
   ....
  }

  @PostMapping(path = "/users/")
  public User createUser(@RequestBody User user) {
   ....
  }

  @DeleteMapping(path = "/users/{id}")
  public void deleteUser(@PathVariable Long id) {
   ....
  } 

Should the trailing slash be removed?


Solution

  • The following urls:

    http://example/foo
    http://example/foo/
    

    Are NOT the same url. Caches will store them separately. So in that sense there is a real difference. Normalizing URLs will not strip them.

    Every URI (ending with slash or not) will point to a resource.

    As far as I know there is no specific recommendation to use either. Some protocols (such as WebDAV) use it to suggest that URLs ending with a slash imply that it's a collection.

    One small benefit of ending with a slash is that relative URLs inside the document (that don't start with slash) will refer to items in the collection. Taking advantage of this means that clients need to correctly resolve relative urls, which is not always true.

    Most APIs I've seen don't end with slashes. To some people, ending with a slash (and requiring this) might be surprising behavior.

    No official sources, because I don't think they exist. I'm fairly deep into standards, so I'm reasonably confident about this.