49

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?

Dexter
  • 4,036
  • 3
  • 47
  • 55
Stephen
  • 868
  • 1
  • 9
  • 18

2 Answers2

39

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.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • 1
    I agree, but unfortunately some have strong opinions in this area and I am tired of changing code one way or the other. I personally have no dog in this fight. – Stephen May 01 '20 at 17:19
  • 5
    @Stephen, frankly this sounds like a tabs vs spaces thing ;) If this is causing grief at your company, my feeling is that this is a culture problem, not a technical one. – Evert May 01 '20 at 17:26
  • 1
    This becomes relevant when the server wants (needs) to send back HTTP Location containing a relative reference path, for example in the response for a POST on a collection, together with a 201 Created. For example 'POST: https://example.com/users' with our without slash, You would return either a Location of 'users/123' or just '123' depending on the ending slash of the URI ``` For the second ``` Location: 123 ``` https://en.wikipedia.org/wiki/HTTP_location – Vlad Ionut Cananau Oct 20 '21 at 14:00
26

Is there an authoritative position I can cite when it comes to a trailing slash on a Restful URI?

The authoritative reference on URI is RFC 3986. Section 3.3 includes the production rule for segments.

/users

This URI has a path /users which includes one segment: "users"

/users/

This URI has a path /users/ which includes two segments: "users" and an empty segment.

REST clients should treat /users and /users/ as two distinct identifiers - so for instance each will have a different cache entry.

REST doesn't offer any sort of opinion on when to use either of these, or when you might choose to use both. That's part of the point, that the authority (server) can assign URI to resources any way that it likes. As far as everyone else is concerned, the identifier is opaque.

Which means that the URI spellings that you use only need to conform to local spelling conventions.

Rails Routing from the Outside In describes one possible convention that you might adopt locally: the "collection" uses the one segment spelling, and the members of the collection use two segment spellings.

Using that convention /users would refer to the collection, and /users/, as far as I can tell, wouldn't be used.

In a domain where it would make sense for a member to have an empty id, then we might expect that member to have the identifier /users/.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • I agree there seems to be no official specification on trailing slash. Starting a couple-of-years-ago there was push against the trailing slash as a standard. It also seems at-that-time the DeFacto standard was the trailing slash when you conceptually had a resource like my example with "users". That way everything was a slash after “users”. So: “Put Post and Get (get all), Delete (delete all)” had consistent syntax. I am trying to determine where this push came from and why? Is this a case of the most vocal wins? – Stephen May 04 '20 at 01:00