What is a URI?
A URI is a Universal Resource Identifier, identifying where a specific resource can be found, such as a page or a document. They are used in REST APIs to address resources to developers using an API. You may have seen URIs, URLs, and URNs in networking discussions before, but how do we tell them apart?
- The URI (Universal Resource Identifier) is a string identifier that refers to a resource on the internet, like a specific document.
- The URL (Uniform Resource Locator) is the location of the resource on the web, and it tells you how to access the resource (`https://`, for example)
- The URN (Uniform Resource Name) is a unique identifier for a single object, meant to be long-term even when the resources the URN identifies are gone.
Parts of the URI Syntax
A URI is an identifier with syntax that looks like this: `scheme:[//authority]path[?query][#fragment]`. Let's have a look at the parts of the URI.
- Scheme: This identifies _how_ the resource is accessed. Examples include http, file, ftp, data, and irc.
- Authority: Authority is optional and preceded by two slashes `//` like a comment. It contains the subcomponents `userinfo`, `host`, and `port`.
- Path: A sequence of path segments separated by a slash(`/`). The URI always specifies it; however, the specified path may be empty or of 0 lengths.
- Query: An optional component preceded with a question mark (`?`) that contains [non-hierarchical data](https://www.javatpoint.com/uri-vs-url0).
- Fragment: An optional component preceded by a hash (`#`) symbol. It provides direction to a secondary resource.
URI Syntax Best Practices
URIs are common resource locations in REST APIs, so naming them descriptively is considered a best practice. A developer using your REST API may not know what a resource does or what it contains, so naming it a string of letters and numbers is not helpful to them. Consider naming it descriptively, for example, `http://api.example.com/louvre/leonardo-da-vinci/mona-lisa`. This follows the URI syntax above, while giving new users a clear path to follow.
Conclusion
URIs and URLs can be difficult to keep distinct in your mind. Here's what it boils down to: All URLs are URIs, but not all URIs are URLs. This is because URLs define how to access a resource in addition to its location, while URIs are only a string identifier for a resource location. We hope this helps!